Lecture4 Polygon Fill Algorithms - معدل
Lecture4 Polygon Fill Algorithms - معدل
Algorithms
Definition
• Polygon is a closed figure with many vertices and edges (line segments),
and at each vertex exactly two edges meet and no edge crosses the other.
Types of Polygon:
• Regular polygons
» No. of edges equal to no. of angles.
• Convex polygons
» Line generated by taking two points in the polygon must lie
within the polygon.
• Concave polygons
» Line generated by taking two points in the polygon may lie
outside the polygon.
Polygon Filling
• Polygon filling: Highlights all pixels inside the polygon.
Two methods:
Boundary Fill
• Seed Fill
Flood Fill
• Scan Line
Seed Filling:
In this method a particular seed point is picked and we
start filling upwards and downwards pixels until boundary
is reached. seed fill method is of two types: boundary fill
and flood fill.
Basic Filling Algorithm
The basic filling algorithm is commonly used in
interactive graphics packages, where the user
specifies an interior point of the region to be filled.
4-connected pixels
4
Types of Basic Filling Algorithms
Boundary Fill Algorithm
◦ For filling a region with a single boundary color.
◦ Condition for setting pixels:
Color is not the same as border color
Color is not the same as fill color
Flood Fill Algorithm
◦ For filling a region with multiple boundary colors.
◦ Here we don’t have to deal with boundary color
◦ Condition for setting pixels:
Coloring of the pixels of the polygon is done with
the fill color until we keep getting the old interior
color.
5
Boundary Fill Algorithm
• An approach to area filling is to start at a point
inside a region and paint the interior outward toward
the boundary.
• If the boundary is specified in a single color, the fill
algorithm processed outward pixel by pixel until the
boundary color is encountered.
• A boundary-fill procedure accepts as input the
coordinate of the interior point (x, y), a fill color,
and a boundary color.
6
Boundary Fill Algorithm
The following steps illustrate the idea of the
recursive boundary-fill algorithm:
7
Boundary Fill Algorithm
8
Area Fill Algorithm
• There are two methods for processing
neighboring pixels from a current point.
1. Four neighboring points.
– These are the pixel positions that are right, left,
above, and below the current pixel.
– Areas filled by this method are called 4-
connected.
Area Fill Algorithm
2. Eight neighboring points.
– This method is used to fill more complex figures.
– Here the set of neighboring points to be set
includes the four diagonal pixels, in addition to the
four points in the first method.
– Fill methods using this approach are called 8-
connected.
Boundary Fill Algorithm (Code)
void boundaryFill(int x, int y,
int fillColor, int borderColor)
{
getPixel(x, y, color);# fetch color
if ((color != borderColor)
&& (color != fillColor)) {
setPixel(x,y);# to give color on particular
pixel
boundaryFill(x+1,y,fillColor,borderColor);
boundaryFill(x-1,y,fillColor,borderColor);
boundaryFill(x,y+1,fillColor,borderColor);
boundaryFill(x,y-1,fillColor,borderColor);
}
}
11
Boundary Fill Algorithm
4-connected (Example) امل+ك
Start Position
12
Boundary Fill Algorithm
4-connected (Example)
3
2
1
1
2 3
13
Boundary Fill Algorithm
4-connected (Example)
4
2
1
1 4
2
14
Boundary Fill Algorithm
4-connected (Example)
2
1
1
2
15
Boundary Fill Algorithm
4-connected (Example)
5
1
5 1
16
Boundary Fill Algorithm
4-connected (Example)
17
Boundary Fill Algorithm
4-connected (Example)
18
Boundary Fill Algorithm
8-connected (Example)
Start Position
19
Boundary Fill Algorithm
8-connected (Example)
5
4
4 1 5 3
2 3 2
1
20
Boundary Fill Algorithm
8-connected (Example)
6
6 4
4 1 3
2 3 2
1
21
Boundary Fill Algorithm
8-connected (Example)
8
7 8
7
4
4 1
3
2 3
2
1
22
Boundary Fill Algorithm
8-connected (Example)
12
11 9 12 11
7 10 10
9
4 1 7
2 3 4
3
2
1
23
Boundary Fill Algorithm
8-connected (Example)
11
11 9
10
7 10
9
7
4 1
4
2 3
3
2
1
24
Boundary Fill Algorithm
8-connected (Example)
9 10
7 10 9
7
4 1 4
2 3 3
2
1
25
Boundary Fill Algorithm
8-connected (Example)
9
9
7
7
4
4 1
3
2 3
2
1
26
Boundary Fill Algorithm
8-connected (Example)
7 7
4
4 1 3
2 3 2
1
27
Boundary Fill Algorithm
8-connected (Example)
4
4 1 3
2 3 2
1
28
Boundary Fill Algorithm
8-connected (Example)
3
1
2
2 3
1
29
Boundary Fill Algorithm
8-connected (Example)
1 2
2 1
30
Boundary Fill Algorithm
8-connected (Example)
1
1
31
Using the Boundary Fill Algorithm to fill
the region in below figure using 4-
connected
8-connected (Example)
32
Flood fill algorithm:
34
Flood Fill Algorithm
We start from a specified interior pixel (x, y) and
reassign all pixel values that are currently set to a
given interior color with the desired fill color.
If the area has more than one interior color, we
can first reassign pixel values so that all interior
pixels have the same color.
Using either 4-connected or 8-connected
approach, we then step through pixel positions
until all interior pixels have been repainted.
35
Area Fill Algorithm
• We start from a specified interior point (x, y)
and reassign all pixel values that are currently
set to a given interior color with the desired
fill color.
• If the area we want to paint has more than
one interior color, we can first reassign pixel
values so that all interior points have the same
color.
Area Fill Algorithm
• Using either a 4-connected or 8-connected
approach, we then step through pixel
positions until all interior points have been
repainted.
1/2 - 39
Flood Fill
• 1. Region is a patch of like-colored pixels
• 2. A seed pixel is set and a range of colors is
defined
• 3. Check if the pixel is in the color range
• 4. If yes, fill it and make the neighbors news
seeds
1/2 - 40
1/2 - 41
1/2 - 43
1/2 - 44
Improving seed fill
Shortcomings of boundary fill and flood fill:
• Time
• Large number of recursive calls
• Pixels might be considered more than once
(test if set, test if inside)
• Memory
• Don’t know how big the fill list should be
• Could be all of the image pixels
• More if our algorithm allows us to consider a pixel more than once
1/2 - 45
Boundary-Fill Flood-Fill Basis for
Algorithm Algorithm comparison
Basic
Painting process
Memory
consumption
Speed
Algorithm
Complexity
Thank You
46