Flood Fill Algorithm in C and C++

Flood Fill Algorithm in C and C++

The Flood Fill algorithm in C is a technique used in computer graphics to fill enclosed areas with a chosen color. Starting from a seed point, neighboring points are recursively filled until a boundary color is encountered, efficiently coloring large regions.

Difference between Flood Fill and Boundary Fill

The main difference between Flood fill algorithm and Boundary Fill algorithm are explaining through the table.

Flood Fill AlgorithmBoundary Fill Algorithm
Area filling is started from a point and it replaces the old color with new color.Area filling is started inside a point within a boundary region and fill the region with specified color until it reaches at boundary.
It is used when we are unable to define the boundary and if there are many color boundary of a region.It is used in interactive package where we can specified region boundary easily.
Algorithm stopped when all old color pixel fill with new color.Algorithm stopped when the entire region covered with boundary fill.
It takes more time then boundary fill.It is less time consuming.
It searches for old colors.It searches for boundary color.

Explain Flood Fill Algorithm

  •  Start at a given seed point (x, y).
  •  Obtain the color of the pixel at (x, y) and store it as oldColor.
  •  If the pixel at (x, y) is not the oldColor or it’s already filled with the desired color (fillColor), stop the process.
  •  Set the pixel at (x, y) to the fillColor.
  •  Recursively perform the following for the four neighboring pixels: (x + 1, y), (x – 1, y), (x, y + 1), and (x, y – 1):
  • Check if the neighboring pixel’s color is the same as oldColor.
  • If yes, repeat steps 3 to 5 for that neighboring pixel.
  •  The process will continue recursively until all connected pixels of the same oldColor are changed to fillColor.

Flood Fill Algorithm in C

Output

Flood Fill Algorithm

 

Flood Fill Algorithm in C++

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top