Program to implement flood fill algorithm

/* Flood fill program */

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void flood(int,int,int,int);
void main()
{

int driver=DETECT,mode;
initgraph(&driver,&mode,” “);
int x,y,i,r;
cout<<“Enter the x coordinate:\n”;

cin>>x;
cout<<“Enter the y coordinate:\n”;
cin>>y;
cout<<“size”;
cin>>r;
for(i=x-(r/2);i<=x+(r/2);i++)
{
putpixel(i,y+r/2,6);
putpixel(i,y-r/2,6);
}
for(i=y-(r/2);i<=y+(r/2);i++)
{
putpixel(x+r/2,i,1);
putpixel(x-r/2,i,1);
}
flood(x,y,2,BLACK);
getch();
closegraph();
}
void flood(int x,int y,int fillcolor,int oldcolor)
{

if(getpixel(x,y)==oldcolor)
{
setcolor(5);
putpixel(x,y,fillcolor);
flood(x+1,y,5,oldcolor);
flood(x-1,y,5,oldcolor);
flood(x,y+1,5,oldcolor);
flood(x,y-1,5,oldcolor);
}
}

Mohit Arora
Follow me