Tuesday, May 5, 2009

C Program to Implement Flood Fill Algorithm

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

void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);

}
}

void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);

fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);

}
}


void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];

printf("\n\n\tEnter the seed pt. : ");
scanf("%d%d",&x,&y);


cleardevice();
setcolor(WHITE);

for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}

fill_right(x,y);
fill_left(x-1,y);

getch();
}

/*SAMPLE INPUT*/
/*Enter the number of edges of polygon 4

X0 Y0 = 50 50
X1 Y1 = 200 50
X2 Y2 = 200 300
X3 Y3 = 50 300

Enter the seed point 100 100*/

9 comments:

Anonymous said...

good work.. thnx, i needed it lyk hell.. :)

Anonymous said...

fukin a...

Anonymous said...

thanks.. i realy need it..

Anonymous said...

can you pls provide its explanation

Neil Roy said...

This is fine, but you risk over flowing the stack. I recommend looking into newer non-recursive methods.

Anonymous said...

plzzzzzzzz tell me how to fill color in custom created polygon like rectangle made with lines

Ajeet Khan said...
This comment has been removed by the author.
Ajeet Khan said...
This comment has been removed by the author.
Ajeet Khan said...

Great program. I found a simplified program here