Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

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

1. Which pixels should be filled in?

2. Which happened to the top pixels? To the rightmost pixels?

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.

General 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!

Illustration 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 i+1.

scan lines

span

span

span span

Filling the Spans


Span-filling is an important step in the whole polygon-filling algorithm, and i is implemented by a three-step process: 1. 2. 3. Find the intersections of the scan line with all edges of the polygon. Sort the intersections by increasing x coordinates. 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?

Parity (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
odd odd odd odd

even
even

Implementation of the Ideas

: intersection points between scan lines and edges (span extrema). : interior pixels

Implementation of the Ideas (cont.)

Filled pixels using the strictly inside principle.

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?

A: The strictly inside rule:

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.

Bottom edges are drawn

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

Four Elaborations (cont.)

G H

F I E C D J

G H

E C D

Four 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.

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.

Edge Coherence (cont.)


m 6 1 5 53 2
4 4 5 1 5

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.

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

Catching 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

An Example of the Active Edge Table


AET pointer F
9

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.

An 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 scanlines. 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.

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

Scan-Line Algorithm (cont.)


1. Set y to the smallest y coordinate that has an entry in the ET, that is, y for the first nonempty bucket.
2. Initialise the AET to be empty. 3. Repeat until the AET and ET are empty. 3.1 3.2 Move from ET bucket y to the AET those edges whose ymin= y (entering edges). Remove from the AET those entries for which y=ymax (edges not involved in the next scan line), then sort the AET on x (made easier because ET is pre-sorted).

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:

You might also like