CG 8
CG 8
Polygon Filling
Topic
Covered
Polygon
Types
Inside – Outside
Test
Polygon Filling
Techniques
Algorithms
Example
What is a Polygon….?
All interior angles lesser than 180°. At least one interior angle is greater than < 180°
180°.
Convex
All of its lines curve outside At least one line curve is inside.
One thing you may note about a convex If you try the same thing with a concave
shape is that, no matter where you draw shape it can pass through more than
a line that passes through the shape, it two of the lines
will always pass through only two of the
lines or polygons making up the shape > 180°
Concave
CONVEX
POLYGON:
CONCAVE
POLYGON:
Inside – Outside
Test
Even – Odd Test
Winding Number
Method
out
in
out
in
Even – Odd
Test
Also known as “Crossing Number Method”
Construct a line segment which crosses the given Polygon and then count
the number of time a line crosses the Polygon Boundary.
If the crossing number is odd then the line is inside the Polygon, else
outside.
out
Counts two
Case B
Winding Number
Method
Instead of just counting the number of intersections, each edge crossed is
given a direction number.
Value of the direction number is :
1, (Y <Y )
start end
-1, (Ystart > Yend)
Point is inside the Polygon if the sum of direction numbers is non-zero, else
outside.
Example
-1
1
Filling a Polygon is the process of coloring every pixel that comes inside
the Polygon region.
Techniques:
✔ Boundary Fill Method
✔ Flood Fill Method
✔ Scan – Line Fill Method
Boundary Fill Method
(X-1, (X+1,
(X, Y+1) (X, Y+1)
Y+1) Y+1)
(X+1,
(X, (X-1, Y-1) (X,
Y-
Y-1) Y-1)
1)
The 4-connected pixel technique failed to fill the area as marked in the
following figure which won’t happen with the 8-connected technique.
4 – Connected Example
Start Position
1
2 3
14
2
1
2
5
1
1
8 – Connected Example
Start Position
4 1
5
2 3
6
4
3
1
2
7
8
4
3
1
2
11 9
12
7 10
4
3
1
2
11
9 10
7
4
3
1
2
9
7 10
4
3
1
2
9
7
4
3
1
2
7
4
3
1
2
4
3
1
2
1
2 3
1
2
1
Algorithm: 4 Connected
Fill
Boundary
∙Flood fill: expand and fill region while you find color
interior
Interior Fill
In
brief: Fill and Boundary Fill are algorithms used
•Flood
for colouring a given figure
with a chosen colour
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7
8 9 10
Scan - Line Base
Approach
10 Scan Line
0
2 4 6 8 10 12 14 16
Scan - Line Draw Polygon
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
10
9
8
7
Filling Process 6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Scan - Line Filling Process
10
9
8
7
6
5
4
3
2
1
0
0 1 2 3 4 5 6 7 8
9 10
Example
Initially, each vertices of the polygon is given in the form of (x,y) and is in
an ordered array as such:
Unfilled, the polygon would look like this to the human eye:
Steps
1.In order to fill a polygon, we do not want to have to determine the type of
polygon that we are filling.
2. The basic concept of the scan-line algorithm is to draw points from edges
of odd parity to even parity on each scan-line.
3.scan-line: A scan-line is a line of constant y value.
Algorithm
Edges with the same minimum y values are sorted on minimum x values
as
follows:
1. Place the first edge with a slope that is not equal to zero in the global
edge table.
2. If the slope of the edge is zero, do not add that edge to the global edge
table.
3.Initializing Parity
Parity is initially set to even. Yet now no edges has crossed.
The active edge table will be used to keep track of the edges that are
intersected by the current scan-line. This should also contain ordered
edges.
This is initially set up as follows:
1. The global edge table is ordered on minimum y and x values, search
through the global edge table and, for each edge found having a minimum
y value equal to the current scan-line.
2. Append the edge information in the AET for the
a. Maximum y value
b. X value
c. 1/m
3.Do this until an edge is found with a minimum y value greater than the scan
line value.
The active edge table will now contain ordered edges of those edges that
are being filled as such:
Filling the Polygon
Filling the polygon involves deciding whether or not to draw pixels, adding
to and removing edges from the active edge table, and updating x values for
the next scan-line.
Starting with the initial scan-line, until the active edge table is empty, do
the following:
a. Draw all pixels from the x value of odd to the x value of even parity edge
pairs.
b. Increase the scan-line by 1.
c. Remove any edges from the active edge table for which the maximum y
value is equal to the scan_line.
a. Update the x value for each edge in the active edge table using the
formula x1 = x0 + 1/m. (This is based on the line formula and the fact
that the next scan-line equals the old scan-line plus one.)
b. Remove any edges from the global edge table for which the minimum
y value is equal to the scan-line and place them in the active edge
table.
c. Reorder the edges in the active edge table according to increasing x
value. This is done in case edges have crossed.
Scan Line 10
The polygon is now filled as
follows:
Scan Line
11
i. Increase the scan-line by 1.
ii. Update x1 = x0 + 1/m
The polygon is now filled as
follows:
The polygon is now filled as
follows:
Scan Line
15
The polygon is now filled as
follows:
Scan Line
16
i. Next scan Line value is 16.
ii. This is equal to maximum value of y in AET. So we will remove these edges
whose maximum y value is 16 from AET.
We then need to update the x values for all remaining
edges.
Now we can add the last edge from the global edge table to the active edge
table since its minimum y value is equal to the next scan-line. The active
edge table now look as follows (the global edge table is now empty):
Now that we have filled the polygon, let's see what it looks like to the naked
eye:
Scan - Line
Alogorithm
1. Set y to the smallest y coordinate that has an entry in the ET; i.e, y for the
first nonempty bucket.
2. Initialize the AET to be empty.
3. Repeat until the AET and ET are empty:
3.1 Move from ET bucket y to the AET those edges whose y_min = y
(entering edges).
3.2 Remove from the AET those entries for which y = y_max (edges not
involved in the next scanline), the sort the AET on x (made easier
because ET is presorted).
3.3 Fill in desired pixel values on scanline y by using pairs of x
coordinates from AET.
3.4 Increment y by 1 (to the coordinate of the next scanline).
3.5 For each nonvertical edge remaining in the AET, update x for the new
y.
References
Book Reference
Computer Graphics –Neetu Agarwal
Graphics Programming in C – Roger Stevens
Web Reference
www.wikipedia.org
www.tutorialspoint.com