Sei sulla pagina 1di 3

Flood Fill Algorithm for Polygon Filling

Theory:
Polygon Filling:

Filling a polygon means highlighting all the pixels which lie inside the polygon with any
color other than the background color.

Polygons are easier to fill since they have linear boundaries.

There are two basic approaches use to fill the polygon

Flood fill algorithm

Scan line algorithm

Flood fill algorithm:

One way to fill the polygon is to start from given seed point known to be inside the
polygon and highlighting out word from this point, i.e. neighboring pixels until we
encounter the boundary pixels.

This approach is called Flood Fill because color flows from the seed pixel until reaching
the polygon boundary

In this method the edges of the polygon are drawn then starting with some seed at any
point inside polygon, we examine neighboring pixels to check whether the boundary
pixel is reached.

If the boundary pixels are not reached, pixels are highlighted and the process is
continued until boundary pixels are not reached. Since this process involves checking of
the boundaries this method is called boundary fill method.

Boundary defines regions may be either 4-connected or 8-connected as shown in fig.

4-connected region

8-connected region
By- Prof.U.N.Abhonkar,Sandip Polytechnic

If region is 4-connected then every pixel in the region may be reached by combination
moves in 4 directions: Left, Right, Up, Down

If region is 8-connected then every pixel in the region may be reached by combination
moves in 2 horizontal, 2 vertical and 4 diagonal directions.

In some cases 8-connected flood fill algorithm is more accurate than 4-connected flood fill
algorithm

The following procedure illustrates the recursive method for filling a 4-connected region
with color specified in parameter fill color up to a boundary color

Procedure for Boundary fill algorithm:


Boundary fill(x,y,f_color,b_color)
{
If(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)
{
putpixel(x,y,f_color)
Boundary fill(x+1,y,f_color,b_color);
Boundary fill(x,y+1,f_color,b_color);
Boundary fill(x-1,y,f_color,b_color);
Boundary fill(x,y-1,f_color,b_color);
}
}
Procedure for Flood fill algorithm:
Floodfill(x,y,old_color,new_color)
{
If(getpixel(x,y)==old_color)
{
putpixel(x,y,new_color);
Floodfill(x+1,y,old_color,new_color);
Floodfill(x-1,y,old_color,new_color);
Floodfill(x,y+1,old_color,new_color);
Floodfill(x+1,y+1,old_color,new_color);
Floodfill(x-1,y-1,old_color,new_color);
Floodfill(x+1,y-1,old_color,new_color);
Floodfill(x-1,y+1,old_color,new_color);
}
}
By- Prof.U.N.Abhonkar,Sandip Polytechnic

C code for Flood fill algorithm:


// flood fill algorithm
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
void main()
{
void floodfill(int,int);
int gd=DETECT,gm,x,y,x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
printf(" \n Enter coordinates of rectangles ");
printf(" \n (X1,Y1,X2,Y2): : ");
scanf("%d%d%d%d ",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
printf(" \n Enter coordinates of seed points ");
scanf("%d%d",&x,&y);
floodfill(x,y);
getch();
closegraph();
}
void floodfill(x,y)
{
If(getpixel(x,y)!=WHITE && getpixel(x,y)!=RED)
{
putpixel(x,y,RED);
floodfill(x+1,y);
floodfill(x,y+1);
floodfill(x-1,y);
floodfill(x,y-1);
}
}
By- Prof.U.N.Abhonkar,Sandip Polytechnic

Potrebbero piacerti anche