To Implement Flood Fill Algorithm
-
Upload
sanjaylulla -
Category
Documents
-
view
13 -
download
1
description
Transcript of To Implement Flood Fill Algorithm
Aim: To Implement Flood Fill AlgorithmTheory: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 stack-based (recursive) flood-fill implementation (for a two-dimensional array) goes as described below algorithm. 4-Connected Region
Fig. a:Four Connected ApproachFigure 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. North2. East3. West4. SouthThese 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 ApproachThe 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. North2. East3. West4. South5. North-East6. North-West7. South-East8. 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);}} 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 (x+1, y+1, fill, boundary);floodfill8 (x-1, y-1, fill, boundary);floodfill8 (x-1, y+1, fill, boundary);floodfill8 (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.2 | Page