 ### Similar presentations

Polygons A polygon is a many-sided planar figure composed of vertices and edges. A polygon is bounded (finite area) and closed (includes boundary). Vertices are represented by points (x,y). Edges are represented as line segments which connect two points, (x1,y1) and (x2,y2). P = { (x i, y i ) } i=1,n E3 (x3,y3) E2 (x2,y2) E1 (x1,y1) Larry Hodges

Polygons: Complex vs Simple Zachary Wartell A simple polygon – edges only intersect a vertices, no coincident vertices A complex polygon – edges intersect and/or coincident vertices A B C C E F G H AB,E C D F A B C A B C D E

Simple Polygons: Convex and Concave Convex Polygon - For any two points P 1, P 2 inside the polygon, all points on the line segment which connects P 1 and P 2 are inside the polygon. –All points P = uP 1 + (1-u)P 2, u in [0,1] are inside the polygon provided that P 1 and P 2 are inside the polygon. Concave Polygon - A polygon which is not convex. Larry Hodges, Zachary Wartell

Inside vs Outside Polygon: Odd-Parity Test Larry Hodges, Zachary Wartell -Is point P inside polygon? -Count # times a ray from P intersects edge of polygon -odd count implies inside -When crossing a vertex, if the vertex’s two edges are on same side of line then count it twice, else count it once. Count onceCount twice or

Filling Concave Polygons Fill the polygon 1 scanline at a time Determine which pixels on each scanline are inside the polygon and set those pixels to the appropriate value. Look only for those pixels at which changes occur. Larry Hodges, Zachary Wartell

Scan-Line Algorithm Larry Hodges, Zachary Wartell For each scan-line: -Find the intersections of the scan line with all edges of the polygon. -Sort the intersections by increasing x-coordinate. -Fill in all pixels between pairs of intersections. Possible Problems 1.Horizontal edges ==> Ignore 2.Vertices ==> If local max or min, then count twice, else count once. (This is implemented by shortening one edge by one pixel.) 3.Calculating intersections is slow. 2 4 6 8 For scan-line number 7 the sorted list of x-coordinates is (1,3,7,9) Therefore fill pixels with x- coordinates 1-3 and 7-9.

Edge Coherence Larry Hodges, Zachary Wartell -Not all polygon edges intersect each scanline. -Many edges intersected by scanline y k will also be intersected by scanline y k+1 -Nearby pixels on given edge and span have similar coordinates, attributes and equation components

“scan-line aligned” trapezoids Zachary Wartell T1 T2 T3 T4 T5 T6 -break up problem into simpler ”scan-line aligned” trapezoids - only two side edges: left, right left edge of T2 right edge of T2

Edge Table Zachary Wartell T1 T2 T3 T4 T5 T6 -sort vertices by y coordinate building Edge Table (ET) y max y=0 A B C D F E G H y D,C yGyG yEyE yByB yHyH yAyA AH AB HG BC EF ED GF ET not needed

Scan AET with AEL Zachary Wartell T1 T2 T3 T4 T5 T6 -scan up through ET and track Active Edge Table (AET). At each scan-line fill in span of pixel for each edge pair. y max y=0 A C D F E G H yGyG yEyE yByB yHyH yAyA AH AB HG BC EF ED GF ET AET k (k = 1..10) [(AH,AB)] [(HG,AB)] [(HG,BC)] [(HG,EF), (DE,BC)] [(GF,EF), (DE,BC)] [(DE,BC)] B

How do we fill trapezoids? Zachary Wartell -Need to generate pixel coordinates of left and right edges of edge pair-- (lx,ly) and (rx,ry) -Need to fill pixel in span between current rx 0, lx 0 y0y0 rx 2 rx 3 lx 1, lx 2, lx 3 H G y1y1 y2y2 y3y3 rx 1

Rasterize Edge Zachary Wartell -From y=mx + b we have: y k+1 = y k +1, x k+1 = x k + 1 / m = x k + Δx / Δy - fractions yuck! -So split into integer and fraction and look at fraction numerator: x k+1 = x k + (Δx div Δy) + (Δx mod Δy) / Δy integer fraction x k = xi k + xn k / Δ y, xi is integer component xn is x’s fraction’s numerator x k=0 y k=0 ykyk y k+1 xkxk x k+1 … … B

Rasterize Edge Larry Hodges, Zachary Wartell -Base Case: xi = x 0, xn = 0 -Iteration Case: xi is integer component, xn is fraction numerator xi := xi + (Δx div Δy) xn := xn + (Δx mod Δy) if xn ≥ Δy then xi := xi + 1 xn := xn – Δy endif A=(6,1) B=(13,4) Δx=7 Δy=3 Δx div Δy= 2 Δx mod Δy= 1 k y xi xn x (=xi+xn/Δy) 0 1 6 0 6 1 2 8 1 8 1/3 2 3 10 2 10 2/3 3 4 13 0 13 0/3 (8,2) (10,3) Warning: H&B doesn’t handle Δx > Δy

Rasterization Special Cases Larry Hodges, Zachary Wartell -Previous slide truncated to obtain integer x coordinate, alternatively use rounding -you end up with a fraction (comparing xn to Δy/2) and must multiply xn by 2 to get all integer operations -Often polygons share edges, to avoid writing the same pixel twice possibly: -horizontal edge – only drawn when on top -right edge – round up -left edge – round down, if xn = 0

Rasterization Special Cases Larry Hodges, Zachary Wartell -Edge Shortening Trick: -Recall Odd-Parity Rule Problem: -Implement “Count Once” case with edge shortening: C B A B C B A B' B B' x A,y B’,1/m AB x C,y B’,1/m CB

Splitting Concave Polygons – Concept  Concave can be split into Convex Polygons – this simplifies Edge Table and Active Edge Table in polygon rasterizer (only one scan-line trapezoid is active at a time!)  Identify: look for interior angle greater than 180º - cross product signs will differ X Y E1E1 E2E2 E3E3 E4E4 E5E5 E6E6 (E 1 X E 2 ) z > 0 (E 2 X E 3 ) z > 0 (E 3 X E 4 ) z < 0 (E 4 X E 5 ) z < 0 (E 5 X E 6 ) z > 0 (E 6 X E 1 ) z > 0 i j k x0 y0 z0 x1 y1 z1 ©Zachary Wartell - 1/26/2005

Splitting Concave Polygons  Edge Vector: Compute Edge Vectors (E k = V k+1 - V k ) traversing polygon counter- clockwise. If cross product of consecutive edge is negative, then split polygon along the embedding line of first edge in cross product  Rotational: put successive edges on x-axis. If next edge has negative x coordinate split polygon on x-axis. X Y E1E1 E2E2 E3E3 E4E4 E5E5 E6E6 X Y E1E1 E2E2 E3E3 E4E4 E5E5 E6E6 X Y X Y ©Zachary Wartell - 1/26/2005

Splitting Convex Polygon to Triangles  Triangle rasterizing is even simpler. Only 3 edges to “sort”. Only fill in 1 or 2 scan-line aligned triangles. Pretty much what all graphics hardware does.  Pick 3 vertices of convex polygon. Split off triangle. Repeat with remaining vertices. Continue until only 3 vertices left. v0v0 v1v1 v2v2 v3v3 v4v4 v0v0 v1v1 v2v2 v3v3 v4v4 v0v0 v1v1 v2v2 v3v3 v4v4 A)B) C) ©Zachary Wartell - 1/26/2005

Filling Irregular Boundaries Larry Hodges, Zachary Wartell  boundary fill: expand and fill region until you reach boundary color  flood fill: expand and fill region while you find interior color Boundary Fill Interior Fill

4 vs 8 connected Larry Hodges, Zachary Wartell  Define: 4-connected versus 8-connected, its about the neighbors

4 vs 8 connected Zachary Wartell  Fill Result: 4-connected versus 8-connected “seed pixel”

Recursive Pixel Boundary Fill  http://www.cosc.canterbury.ac.nz/people/mukundan/cogr/Fill.html http://www.cosc.canterbury.ac.nz/people/mukundan/cogr/Fill.html void boundaryFill4(int x, int y){ if(getPixel(x,y)!=borderColor && getPixel(x,y)!=fillColor ){ setPixel(x,y); boundaryFill4(x+1,y); boundaryFill4(x-1,y); boundaryFill4(x,y+1); boundaryFill4(x,y-1); } }

6 5 4 7 1 2 1 3 1 3 1 2 1 4 1 5 6 1 4 1 5 5 7 4 Recursive Scan-line (boundary) fill Zachary Wartell A B C D Stack

Fill Patterns Larry Hodges, Zachary Wartell Fill patterns can be used to put a noticeable “texture” inside a polygon. A fill pattern can be defined in a 0-based, m x n array. A pixel (x,y) is assigned the value found in: pattern((x mod m), (y mod n)) PatternPattern filled polygon

Halftoning Larry Hodges, Zachary Wartell For 1-bit (B&W) displays, fill patterns with different fill densities can be used to vary the range of intensities of a polygon. The result is a tradeoff of resolution (addressability) for a greater range of intensities and is called halftoning. The pattern in this case should be designed to avoid being noticed. These fill patterns are chosen to minimize banding.

Dithering Larry Hodges, Zachary Wartell Another method to increasing the number of apparent intensities on a 1-bit (B&W) display is dithering. In an ordered dither the decision to turn a pixel on or off at point (x,y) depends on the desired intensity I(x,y) at that point and on an (n by n) dither matrix D n. The dither matrix is indexed from 0 to (n-1) along its rows and columns. Each of the integers 0 to n 2 -1 appears once in the matrix. For instance when n = 4, we have D 4 = 08210 124146 31119 157135 To process the point at (x,y), we first compute i = x MOD 4, j = y MOD 4 Then if I(x,y) > D 4 (i, j) the point (x,y) is turned on; otherwise it is not.

Revisions Larry Hodges, Zachary Wartell 1.1 1.Removed slide 17 “Antialiasing Polygons” (putting anti-aliasing from H&B in separate.ppt) 1.2 1.Fixed error in slide 11 1.4 –Added Slide 23 ‘Recursive Pixel Boundary Fill’ –clarified diagram in slide 24 and noted subtly/error found in textbook.