Professional Documents
Culture Documents
Filling Polygons: - Three Types of Polygons
Filling Polygons: - Three Types of Polygons
1. Simple convex
2. simple concave
3. non-simple (self-intersection)
Convex polygons have the property that intersecting lines crossing it either one (crossing a corner), two (crossing an edge, going through the polygon and going out the other edge), or an infinite number of times (if the intersecting line lies on an edge).
Some Problems
Some Remarks to the 2nd problem Why is the 2nd problem such a big deal? What would happen if we fill the top and right most pixels? Because this will cause double-fill when two rectangles are adjacent. Double-filling brings the following two disadvantages: 1). Inefficient 2). If polygons have different colour, then final colour depends on the order in which the polygons are drawn, and may lead to flicker.
Fill in polygons by computing intersections of boundaries with scan lines, and filling between pairs of intersections
This is the actual algorithm!
scan lines
span
span
span span
Now more questions arise: How do we find and sort the intersections efficiently? How do we judge whether a pixel lying inside or outside the polygon?
even
odd odd odd odd
even
even
: intersection points between scan lines and edges (span extrema). : interior pixels
Four Elaborations to the 2nd Question Q1: Given an intersection with an arbitrary, fractional x value, how do we determine which pixel on either side of that intersection is interior?
Four Elaborations (cont.) Q2: How do we deal with the special case of intersections at integer pixel coordinates? A: Use the criterion we used for avoid conflicting between shared edges: if the leftmost pixel in a span has integer x coordinate, we define it to be interior; if the rightmost pixel has integer x coordinate, we define it to be exterior.
Four Elaborations (cont.) Q3: How do we deal with the special case for shared vertices? A: We count the ymin vertex of an edge in the parity calculation but not the ymax vertex.
A A C
ymin
B ymax C
ymin
B
ymin
Four Elaborations (cont.) Q4: How do we deal with the special case in which the vertices define a horizontal edge? A: Bottom edges are drawn but top edges are not.
Example
Lets apply the rules to scan line 8 below. We fill in the pixels from point a, pixel (2, 8), to the first pixel to the left of point b, pixel (4, 8), and from the first pixel to the right of point c, pixel (9, 8), to one pixel to the left of point d, pixel (12, 8). For scan line 3, vertex A counts once because it is the ymin vertex of edge FA, but the ymax vertex of edge AB; this causes odd parity, so we draw the span from there to one pixel to the left of the intersection with edge CB.
D F a odd E C A B b even c odd d even
G H
F I E C D J
G H
E C D
Edge Coherence
In order to calculate intersections between scan lines and edges, we must avoid the brute-force technique of testing each polygon edge for intersection with each new scan line it is inefficient and slow. Clever Solution: if an edge intersects with a scan line, and the slope of the edge is m, then successive scan line intersections can be found from: xi+1 = xi + 1/m where i is the scan line count. Given that 1/m = (x1 x0)/(y1 y0)
the floating-point arithmetic can be avoided by storing the numerator, comparing to the denominator, and incrementing x when it overflows.
1/m = 2/5
xmin = 3, the sequence is:
2 4 6 1 3 ,3 ,3 4 ,... 5 5 5 5
numerator: denominator:
2 4 5 1
3 2 5
Scan-Line Algorithm
Two sub-problems for polygon filling Find and sort intersections Fill the spans.
FA
E
9
5 2 2 6 4
C EF A B DE
11 10
11 13 0
Ymax x
CD
1 m
As each new scan-line y+1 is encounter, update AET: 1). Remove edges not intersected by y+1 (ymax=y) 2). Add edges intersected by y+1 (ymin=y+1) 3). Calculate new x intersections.
11
10 9 8
9 2 -5/2 EF 11 7 6/4
DE
y coordinate
7 6 5 4 3 2 1 0
11 13 0
CD
9 2 0 FA
3 2 -5/2 AB
5 7 6/4
BC
ymax
1/m
xmin
3.3
3.4 3.5
Fill in desired pixel values on scan line y by using pairs of x coordinates from the AET.
Increment y by 1 (to the coordinate of the next scan line). For each non-vertical edge remaining in the AET, update x for the new y.
Examples:
Solutions: