Title: CA433: Computer Graphics I
1Filling Polygons
- 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).
2Some Problems
1. Which pixels should be filled in?
2. Which happened to the top pixels? To the
rightmost pixels?
3Some 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.
4General Ideas about Polygon Filling
- Rules for shared edges
- A shared vertical edge belongs to the rightmost
of the two sharing shapes. (Therefore, the right
edge of rectangles will not be filled). - A shared non-vertical edge belongs to the upper
shape. (Therefore, the top edge of rectangles
will not be filled). - Fill in polygons by computing intersections of
boundaries with scan lines, and filling between
pairs of intersections - This is the actual algorithm!
5Illustration of the Ideas
A span is the collection of adjacent pixels on a
single scan line which lie inside the primitive.
Coherence literally means to be logically
consistent or connected. Spatial coherence means
that primitives don't change an awful lot if at
all from pixel to pixel within a scan line or
from scan line to scan line. This allows us to
optimise our algorithms. Edge coherence means
that most of the edges that intersect scan line i
also intersect scan line i1.
6Filling the Spans
- Span-filling is an important step in the whole
polygon-filling algorithm, and i is implemented
by a three-step process - 1. Find the intersections of the scan line with
all edges of the polygon. - 2. Sort the intersections by increasing x
coordinates. - 3. Fill in all pixels between pairs of
intersections that lie interior to the polygon. - 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?
7Parity (Odd-Even) Rule
Begin from a point outside the polygon,
increasing the x value, counting the number of
edges crossed so far, a pixel is inside the
polygon if the number of edges crossed so far
(parity) is odd, and outside if the number of
edges crossed so far (parity) is even. This is
known as the parity, or the odd-even, rule. It
works for any kind of polygons.
Parity starting from even
even
even
odd
odd
odd
even
odd
8Implementation of the Ideas
9Implementation of the Ideas (cont.)
Filled pixels using the strictly inside principle.
10Four 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?
A The strictly inside rule
11Four 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.
12Four 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.
13Four 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.
Bottom edges are drawn
Top edges are not
14Example
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.
15Four Elaborations (cont.)
E
16Four Elaborations (cont.)
We deal properly with the horizontal edges by
not counting their vertices. For the figure in
the last slide, consider bottom edge AB. Vertex A
is a ymin vertex for edge JA, and AB does not
contribute. Therefore, the parity is odd and the
span AB is drawn. Vertical edge BC has its ymin
at B, but again AB does not contribute. The
parity becomes even, and the span is terminated.
At vertex J, edge IJ has a ymin vertex but edge
JA does not, so the parity becomes odd and the
span is drawn to edge BC. The span that starts at
edge IJ and hits C sees no change at C because C
is a ymax vertex for BC, so the span continues
along bottom edge CD at D, however, edge DE has
a ymin vertex, so the parity is reset to even and
the span ends. At I, edge IJ has its ymax vertex
and edge HI also does not contribute, so parity
stays even and the top edge IH is not drawn. At
H, however, edge GH has a ymin vertex, the parity
becomes odd, and the span is drawn from H to the
pixel to the left of the intersection with edge
EF. Finally, there is no ymin vertex at G, nor is
there one at F, so top edge FG is not drawn.
17Edge 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 xi1 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.
18Edge Coherence (cont.)
1/m 2/5 xmin 3, the sequence
is numerator denominator
2
4
1
5
19Scan-Line Algorithm
- Two sub-problems for polygon filling
- Find and sort intersections
- Fill the spans.
- AET active-edge table
- store all edges intersected by a scan-line y
sorted by x intersection. Each entry in AET
contains the ymax coordinate of the edge, the x
coordinate of the intersection point, and 1/m. - ET Global Edge Table
-
20Catching Edge/Scan line intersections
- Catch the intersection information in a table
- Edge Table (ET) with all edges sorted by ymin
- Active Edge Table (AET) containing
- The edges that intersect the current scan line
- Their points of intersection
- Sorted by x-coordinate, left to right
-
21An Example of the Active Edge Table
AET pointer
FA
EF
DE
As each new scan-line y1 is encounter, update
AET 1). Remove edges not intersected by y1
(ymaxy) 2). Add edges intersected by y1
(yminy1) 3). Calculate new x intersections.
CD
Ymax x
22An Example for the Global Edge Table
Global edge table (ET) make the addition of
edges to the AET efficient. It contains all edges
sorted by their smaller y coordinate. The ET is
typically built by using a bucket sort with as
many buckets as there are scan-lines. Within each
bucket, edges are kept in order of increasing x
coordinate of the lower endpoint. Each entry in
the ET contains the ymax coordinate of the edge,
the x coordinate of the bottom endpoint (xmin),
and 1/m.
23Scan-Line Algorithm (cont.)
- Set y to the smallest y coordinate that has an
entry in the ET, that is, y for the first
nonempty bucket. - Initialise the AET to be empty.
- Repeat until the AET and ET are empty.
- 3.1 Move from ET bucket y to the AET those edges
whose ymin y (entering edges). - 3.2 Remove from the AET those entries for which
yymax (edges not involved in the next scan
line), then sort the AET on x (made easier
because ET is pre-sorted). - 3.3 Fill in desired pixel values on scan line y
by using pairs of x coordinates from the AET. - 3.4 Increment y by 1 (to the coordinate of the
next scan line). - 3.5 For each non-vertical edge remaining in the
AET, update x for the new y.
24Examples
25Solutions