Flood Fill Algorithm in C and C++

Here you will learn about flood fill algorithm in C and C++.

Flood Fill is a seed fill algorithm similar to Boundary Fill algorithm but sometimes when it is required to fill in an area that is not defined within a single color boundary we use flood fill instead of boundary fill.

For this purpose we can create a function or we can use a predefined function in the graphics.h header file which takes 3 arguments:-

floodfill(x,y,color)

Flood Fill Algorithm in C and C++

4 Connected Region (Image Source)

In Flood Fill algorithm we start with some seed and examine the neighboring pixels, however pixels are checked for a specified interior color instead of boundary color and is replaced by a new color. It can be done using 4 connected or 8 connected region method.

Below we use 4 connected region recursive algorithm to implement this algorithm.

Algorithm

1. Create a function called as floodFill (x,y,oldcolor,newcolor)

2. Repeat until the polygon is completely filled.

3. Stop.

Program for Flood Fill Algorithm in C and C++

C Program

C++ Program

Author Bio:

I am Rahul Maheshwari from India currently pursuing engineering degree in Computer Science. I am passionate about programming and loves to code as much as I can and likes to help people to become better in programming.

Connect with him: Facebook | Linkedin

Comment below if you have queries or found anything incorrect in above flood fill algorithm in C and C++.

12 thoughts on “Flood Fill Algorithm in C and C++”

Leave a Comment

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