Você está na página 1de 3

Aim: To Implement

Algorithm

Flood

Fill

Theory:
Flood fill, also called seed fill, is an algorithm that determines the area
that are connected to a given node in a multi-dimensional array. It is used
in the "bucket" fill tool of paint programs to determine which parts of a
bitmap to fill with color, and in puzzle games such as Puyo Puyo, Lumines,
Magical Drop, and some implementations of Tetris (but not Columns) for
determining which pieces are cleared.
The flood fill algorithm takes three parameters: a start node, a target
color, and a replacement color. The algorithm looks for all nodes in the
array which are connected to the start node by a path of the target color,
and changes them to the replacement color. There are many ways in
which the flood-fill algorithm can be structured, but they all make use of a
queue or stack data structure, explicitly or implicitly. One implicitly stackbased (recursive) flood-fill implementation (for a two-dimensional array)
goes as described below algorithm.

4-Connected Region

Fig. a:Four Connected Approach


Figure shows two methods for proceeding to neighbouring pixels from the
current test position. In the figure, four neighbouring points are tested.
The four neighbouring sides are refered as:
1.
2.
3.
4.

North
East
West
South
1 | Page

These are the pixel positions that are right, left above, and below the
current pixel. Areas filled by this method are called 4-connected. It is
called 4-connected because it is connected to 4 neighbouring regions of
the current pixel.

8-Connected Region:
Seed point

Fig. b:Eight Connected Approach


The second method, shown in figure is used to fill more complex figures.
Here the set of neighbouring positions to be tested includes the four
diagonal pixels. Fill methods using this approach are called 8-connected.
The eight neighbouring sides are refered as:
1.
2.
3.
4.
5.
6.
7.
8.

North
East
West
South
North-East
North-West
South-East
South-West

An 8-connected boundary fill algorithm would correctly fill the interior of


the area defined in figure but the 4 connected boundary fill algorithm
produces partial fill shown.

Algorithm for 4-connected Flood fill :


void floodFill4 (int x, int y, int fillColor, int oldColor)
{
if (getPixel (x, y) == oldColor)
{
setColor (fillColor);
setPixel (x, y);
floodFill4 (x+1, y, fillColor, oldColor);
floodFill4 (x-1, y, fillColor, oldColor);
floodFill4 (x, y+1, fillColor, oldColor);
floodFill4 (x, y-1, fillColor, oldColor);
2 | Page

}
}

Algorithm for 8-connected Flood fill :


void floodFill8 (int x, int y, int fillColor, int oldColor)
{
if (getPixel (x, y) == oldColor)
{
setColor (fillColor);
setPixel (x, y);
floodfill8 (x+1, y, fillColor, oldColor);
floodfill8 (x-1, y, fillColor, oldColor);
floodfill8 (x, y+1, fillColor, oldColor);
floodfill8 (x, y-1, fillColor, oldColor);

floodfill8
floodfill8
floodfill8
floodfill8

(x+1, y+1, fill, boundary);


(x-1, y-1, fill, boundary);
(x-1, y+1, fill, boundary);
(x+1, y-1, fill, boundary);

}
}

Conclusion:
It is used when an area defined with multiple color boundaries.
Flood fill instead of checking the boundary colour it checks the
interior colour (original polygon colour).
It can be implemented using 4connected or 8 connected region
filling.

3 | Page

Você também pode gostar