# Flood Fill Algorithm in C and C++

Here you will find out about flood fill calculation in C and C++.

Flood Fill is a seed fill calculation like Boundary Fill calculation however at times when it is required to

fill in a region that isn’t characterized inside a solitary shading limit we use flood fill rather than a limit fill.

For this reason, we can make a capacity or we can utilize a predefined work in the graphics.h header record which takes 3 contentions:-

floodfill(x,y,color)

In Flood Fill calculation we start with some seed and look at the neighbouring pixels,

anyway pixels are checked for a predetermined inside shading rather than limit shading and is

supplanted by another shading. It tends to be finished utilizing 4 associated or 8 associated locale strategy.

Underneath we utilize 4 associated locale recursive calculation to actualize this calculation.

Calculation

Make a capacity called as floodFill (x,y,oldcolor,newcolor)

``````void floodFill(int x,int y,int oldcolor,int newcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel``````

Rehash until the polygon is totally filled.

Stop.

Program for Flood Fill Algorithm in C and C++

#### C Program

``````#include<stdio.h>
#include<graphics.h>
#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int x,y;

printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
floodFill(x,y,0,15);
delay(5000);
closegraph();

return 0;
}``````

#### C++ Program

``````#include<iostream.h>
#include<graphics.h>
#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int x,y;

cout<<"Enter x and y positions for circle\n";
cin>>x>>y;