Seedfillingalgorithm 180520071431

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

For highlighting all the pixels inside the polygon,

2 approaches can be used-

1. Scan Fill

2. Seed Fill (Boundary Fill, Flood Fill )


Boundary Fill Algorithm
This algorithm picks a point inside the
polygon and starts to fill until it hits the
boundary of the object.

Assumption:

In this algorithm, we assume that color of the


boundary is same for the entire object.
void boundaryfill(int x,int y,int fill_color,int
boundary_color)
{
if(getpixel(x,y)!=boundary_color &&
getpixel(x,y)!=fill_color)
{
putpixel(x,y,fill_color);
boundaryfill(x+1,y,fill_color,boundary_color);
boundaryfill(x-1,y, fill_color,boundary_color);
boundaryfill(x,y+1, fill_color,boundary_color);
boundaryfill(x,y-1, fill_color,boundary_color);
}
}
Sometimes we want to fill in an area that is
not defined within a single color boundary.
We paint such areas by replacing a specified
interior color instead of searching for a
boundary color value. This approach is
called a flood-fill algorithm.
void floodfill(int x,int y,int fill_color,int old_color)
{
if(getpixel(x,y)==old_color)
{
putpixel(x,y,fill_color);
floodfill(x+1,y,fill_color,old_color);
floodfill(x-1,y, fill_color,old_color);
floodfill(x,y+1, fill_color,old_color);
floodfill(x,y-1, fill_color,old_color);
}
}
In brief:

Flood Fill and Boundary Fill are algorithms used for


coloring a given figure with a chosen color

Flood Fill is one in which all connected pixels of a


selected color get replaced by a fill color.

 Boundary Fill is very similar with the difference being


the program stopping when a given color boundary is
found.
Boundary Fill Algorithm /Flood Fill
algorithm
The boundary fill algorithm/ flood fill algorithm can be
implemented by 4-connected pixels or 8-connected pixels.

4-connected 8-connected
4-connected (Example)

Start Position
3
2
1
1
2 3
4
2
1 4
1
2
2
1
1
2
5
5 1
1
1
1
Some region remains unfilled
8-connected (Example)

Start Position
4 1 5
5
2 3
4
3
2
1
6
4 1
6
2 3
4
3
2
1
7 8

8
4 1
7
2 3
4
3
2
1
12
11 9 12
11
7 10
10
9
4 1
7
2 3
4
3
2
1
11 9
11
7 10
10
9
4 1
7
2 3
4
3
2
1
9
7 10
10
9
4 1
7
2 3
4
3
2
1
9
7

9
4 1
7
2 3
4
3
2
1
7

4 1
7
2 3
4
3
2
1
4 1
2 3
4
3
2
1
1
2 3

3
2
1
1
2

2
1
1

1
Comparison
Flood Fill Algorithm Boundary Fill Algorithm
 Flood fill colors an entire area in an  Here area gets colored with pixels
enclosed figure through of a chosen color as boundary this
interconnected pixels using a single giving the technique its name
color  Boundary Fill is very similar with
 So, Flood Fill is one in which all the difference being the program
connected pixels of a selected color stopping when a given color
get replaced by a fill color. boundary is found.
 A flood fill may use an  Boundary fill is usually more
unpredictable amount of memory complicated but it is a linear
to finish because it isn't known how algorithm and doesn't require
many sub-fills will be spawned recursion
 Time Consuming  It is less Time Consuming

You might also like