Computer Graphics Polygon Filling
Computer Graphics Polygon Filling
Polygon Filling
1
Polygon Filling
Types of filling
• Solid-fill
All the pixels inside the polygon’s boundary
are illuminated.
• Pattern-fill
the polygon is filled with an arbitrary
predefined pattern.
2
Polygon Representation
The polygon can be represented by listing its n
vertices in an ordered list.
P = {(x1, y1), (x2, y2), ……., (xn, yn)}.
4
Inside-Outside Tests
when filling polygons we should decide whether a
particular point is interior or exterior to a polygon.
A rule called the odd-parity (or the odd-even rule)
is applied to test whether a point is interior or not.
To apply this rule, we conceptually draw a line
starting from the particular point and extending to a
distance point outside the coordinate extends of the
object in any direction such that no polygon vertex
intersects with the line.
5
Inside-Outside Tests
The point is considered to be interior if the number
of intersections between the line and the polygon
edges is odd. Otherwise, The point is exterior point.
Outside 8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11
Inside
6
The Scan-Line Polygon Fill Algorithm
The scan-line polygon-filling algorithm involves
• the horizontal scanning of the polygon from its
lowermost to its topmost vertex,
• identifying which edges intersect the scan-line,
• and finally drawing the interior horizontal lines with
the specified fill color. process.
7
The Scan-Line Polygon Fill Algorithm
Dealing with vertices
8
The Scan-Line Polygon Fill Algorithm
Dealing with vertices
• When the endpoint y coordinates of the two edges are
increasing, the y value of the upper endpoint for the
current edge is decreased by one (a)
• When the endpoint y values are decreasing, the y value
of the next edge is decreased by one (b)
9
The Scan-Line Polygon Fill Algorithm
Determining Edge Intersections
m = (yk+1 – yk) / (xk+1 – xk)
yk+1 – yk = 1
xk+1 = xk + 1/m
10
The Scan-Line Polygon Fill Algorithm
• Each entry in the table for a particular scan line contains
the maximum y value for that edge, the x-intercept value
(at the lower vertex) for the edge, and the inverse slope of
the edge.
11
The Scan-Line Polygon Fill Algorithm
(Example) Polygon = {A, B, C, D, E, F, G}
Polygon = {(2, 7), (4, 12), (8,15), (16, 9), (11, 5), (8, 7), (5, 5)}
12
The Scan-Line Polygon Fill Algorithm
(Example)
13
The Scan-Line Polygon Fill Algorithm
(Example)
• Vertex B should be split into two vertices B' (xB', 11) and
B(4, 12)
m =( 7 – 12)/( 2 – 4) = 5/2
x'A = 2 + (2/5)( 12 – 1 – 7) = 18/5 = 3.6 4
The vertex B is split to B' (4, 11) and B(4, 12) 15
The Scan-Line Polygon Fill Algorithm
(Example)
17
The Scan-Line Polygon Fill Algorithm
(Example)
18
The Scan-Line Polygon Fill Algorithm
(Example)
19
The Scan-Line Polygon Fill Algorithm
(Example)
20
The Scan-Line Polygon Fill Algorithm
(Example)
21
Boundary Fill Algorithm
• Another 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.
22
Boundary Fill Algorithm
The following steps illustrate the idea of the
recursive boundary-fill algorithm:
23
Boundary Fill Algorithm
24
Boundary Fill Algorithm
4-connected (Example)
Start Position
25
Boundary Fill Algorithm
4-connected (Example)
3
2
1
1
2 3
26
Boundary Fill Algorithm
4-connected (Example)
4
2
1 4
1
2
27
Boundary Fill Algorithm
4-connected (Example)
2
1
1
2
28
Boundary Fill Algorithm
4-connected (Example)
5
5 1
1
29
Boundary Fill Algorithm
4-connected (Example)
1
1
30
Boundary Fill Algorithm
4-connected (Example)
31
Boundary Fill Algorithm
8-connected (Example)
Start Position
32
Boundary Fill Algorithm
8-connected (Example)
4 1 5
5
2 3
4
3
2
1
33
Boundary Fill Algorithm
8-connected (Example)
6
4 1
6
2 3
4
3
2
1
34
Boundary Fill Algorithm
8-connected (Example)
7 8
8
4 1
7
2 3
4
3
2
1
35
Boundary Fill Algorithm
8-connected (Example)
12
11 9 12
11
7 10
10
9
4 1
7
2 3
4
3
2
1
36
Boundary Fill Algorithm
8-connected (Example)
11 9
11
7 10
10
9
4 1
7
2 3
4
3
2
1
37
Boundary Fill Algorithm
8-connected (Example)
9
7 10
10
9
4 1
7
2 3
4
3
2
1
38
Boundary Fill Algorithm
8-connected (Example)
9
7
9
4 1
7
2 3
4
3
2
1
39
Boundary Fill Algorithm
8-connected (Example)
4 1
7
2 3
4
3
2
1
40
Boundary Fill Algorithm
8-connected (Example)
4 1
2 3
4
3
2
1
41
Boundary Fill Algorithm
8-connected (Example)
1
2 3
3
2
1
42
Boundary Fill Algorithm
8-connected (Example)
1
2
2
1
43
Boundary Fill Algorithm
8-connected (Example)
44
Boundary Fill Algorithm
8-connected (Example)
45
Boundary Fill Algorithm
47
Span Flood-Fill Algorithm (example)
11
10
9
8
7
6
5 S
4
3
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
48
Span Flood-Fill Algorithm (example)
11
10
9
8
7
6 2
5 S
4 1
2
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
49
Span Flood-Fill Algorithm (example)
11
10
9
8
7 3
6 2
5 S
4 1
3
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
50
Span Flood-Fill Algorithm (example)
11
10
9
8 5 6
7 3
6 4 2
6
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
51
Span Flood-Fill Algorithm (example)
11
10
9 7
8 5 6
7 3
6 4 2
7
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
52
Span Flood-Fill Algorithm (example)
11
10
9 7
8 5 6
7 3
6 4 2
5 S
5
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
53
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 S
8
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
54
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 S
4 1
4
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
55
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 1
9
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
56
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
10
3
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
57
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
11
3 11
1
2
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
58
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
12
3 11
1
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
59
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
3 11
1
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
60
Span Flood-Fill Algorithm (example)
11
10
9 8 7
8 5 6
7 3
6 4 2
5 9 S
4 10 1
3 11
2 12
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
61
Flood Fill Algorithm
Sometimes we want to fill in (recolor) 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.
62
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.
63