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++.


7 thoughts on “Flood Fill Algorithm in C and C++

    1. prashant banjare

      vishal ek paint brush utha aur screen ko color karde

      Reply
  1. a

    pyramid ke andar ka koi bhi point le lo,uske coordinates function me pass krwa dena ,fir koi bhi filling algo laga do

    Reply
  2. jimbogt P.garden

    bro what the hell this doesnt help me solve HIKING TRAIL PROBLEM bro please help me?

    Reply
    1. Neeraj Mishra Post author

      path of graphics file required to run this program

      Reply
  3. DD

    If you fill with oldColor = newColor won’t it run forever?

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.