AGENDA Polygon Terminology Types of polygons Inside Test

























- Slides: 25

AGENDA Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill Algorithm

A POLYGON Vertex = point in space (2 D or 3 D) Polygon = ordered list of vertices Each vertex connected with the next in the list Last is connected with the first May contain self-intersections Simple polygon – no self-intersections These are of most interest in CG

POLYGONS IN GRAPHICS The main geometric object used for interactive graphics. Different types of Polygons Simple Convex Simple Concave Non-simple : self-intersecting Convex Concave Self-intersecting

POLYGON REPRESENTATION AND ENTERING 4

INSIDE TEST Why ? Want to fill in (color) only pixels inside a polygon What is “inside” of a polygon ? Methods 1. 2. Even –odd method Winding no. method

EVEN –ODD METHOD Case: 2 Case : 1 P P Q P Count number of intersections with polygon edges If N is odd, point is inside If N is even, point is outside Odd P Q Q even Q

WINDING NO. METHOD Every side has given a no. called winding no. Total of this winding no. is called net winding. If net winding is zero then point is outside otherwise it is inside. +1 -1

POLYGON FILLING § Seed Fill Approaches § 2 algorithms: Boundary Fill and Flood Fill § works at the pixel level § suitable for interactive painting apllications § Scanline Fill Approaches § works at the polygon level § better performance 8

SEED FILL ALGORITHMS: CONNECTEDNESS § 4 -connected region: From a given pixel, the region that you can get to by a series of 4 way moves (N, S, E and W) § 8 -connected region: From a given pixel, the region that you can get to by a series of 8 way moves (N, S, E, W, NE, NW, SE, and SW) 9 4 -connected 8 -connected

BOUNDARY FILL ALGORITHM Boundary-defined region Start at a point inside a region Paint the interior outward to the edge The edge must be specified in a single color Fill the 4 -connected or 8 -connected region : 4 -connected fill is faster, but can have problems 10

BOUNDARY FILL ALGORITHM (CONT(. void Boundary. Fill(int x, int y, newcolor, edgecolor( } int current; current = Read. Pixel(x, y; ( if(current != edgecolor && current != newcolor( } putpixel(x, y, newcolor); Boundary. Fill(x+1, y, newcolor, edgecolor; ( Boundary. Fill(x-1, y, newcolor, edgecolor; ( Boundary. Fill(x, y+1, newcolor, edgecolor; ( Boundary. Fill(x, y-1, newcolor, edgecolor; ( { { 11

FLOOD FILL ALGORITHM Interior-defined region Used when an area defined with multiple color boundaries Start at a point inside a region Replace a specified interior color (old color) with fill color Fill the 4 -connected or 8 -connected region until all interior points being replaced 12

FLOOD FILL ALGORITHM (CONT(. void Flood. Fill(int x, int y, newcolor, old. Color( } if(Read. Pixel(x, y) == old. Color( } putpixel(x, y, newcolor); Flood. Fill(x+1, y, newcolor, old. Color; ( Flood. Fill(x-1, y, newcolor, old. Color; ( Flood. Fill(x, y+1, newcolor, old. Color; ( Flood. Fill(x, y-1, newcolor, old. Color; ( { { 13

PROBLEMS WITH SEED FILL ALGORITHM Recursive seed-fill algorithm may not fill regions correctly if some interior pixels are already displayed in the fill color. This occurs because the algorithm checks next pixels both for boundary color and for fill color. Encountering a pixel with the fill color can cause a recursive branch to terminate, leaving other interior pixels unfilled. This procedure requires considerable stacking of neighboring points, more efficient methods are generally employed.

SCAN LINE POLYGON FILLING Interior Pixel Convention Pixels that lie in the interior of a polygon belong to that polygon, and can be filled. Pixels that have centers that fall outside the polygon, are said to be exterior and should not be drawn and filled. Basic Scan Line Algorithm For each scan line: 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.

BASIC SCAN-FILL ALGORITHM For scan line number 8 the sorted list of x-coordinates is (2, 4, 9, 13) (b and c are initially no integers) Therefore fill pixels with x- coordinates 2 -4 and 9 -13.

WHAT HAPPENS AT EDGE ENDPOINT? Intersection points along the scan lines that intersect polygon vertices. Scan line y’ has odd intersection points and intersects four polygon edges Scan line y has even intersection points and intersects five polygon edges

WHAT HAPPENS AT EDGE ENDPOINT? Edge endpoint is duplicated. In other words, when a scan line intersects an edge endpoint, it intersects two edges. Two cases: Case A: edges are on opposite side of the scan line Case B: edges are on the same side of current scan line In Case A, we should consider this as only ONE edge intersection In Case B, we should consider this as TWO edge intersections Scan-line Case A Case B

SPACIAL HANDLING (CONT(. Case 1 Intersection points: (p 0, p 1, p 2? ? ? ( )<-p 0, p 1, p 2) so we can still fill pairwise ->In fact, if we compute the intersection of the scanline with edge e 1 and e 2 separately, we will get the intersection point p 1 twice. Keep both 19 of the p 1.

SPACIAL HANDLING (CONT(. Case 2 However, in this case we count p 1 once )p 0, p 1, p 2, p 3 , (

WHAT HAPPENS AT EDGE ENDPOINT? For Scan line y’ (1, 2, 2, 3) For Scan line y (1, 2, 3, 4)

COHERENCE Coherence: pixels that are nearby each other tend to share common attributes (color, illumination, normal vectors, texture, etc. ). Types of Coherence Span coherence: Pixels in the same scan line tend to be similar. Scan-line coherence: Pixels in adjacent scan line tend to be similar. Edge coherence means that most of the edges that intersect scan line i also intersect scan line i+1. (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. )

EDGE COHERENCE Problem: Calculating intersections is slow. Solution: Incremental computation / coherence Computing the intersections between scan lines and edges can be costly Use a method similar to the midpoint line drawing algorithm y = mx + b, x = (y – b) / m At y = s, xs = (s – b ) / m At y = s + 1, xs+1 = (s+1 - b) / m = xs + 1 / m Incremental calculation: xs+1 = xs + 1 / m

ACTIVE EDGE TABLE A table of edges that are currently used to fill the polygon Scan lines are processed in increasing Y order. Polygon edges are sorted according to their minimum Y. When the current scan line reaches the lower endpoint of an edge it becomes active. When the current scan line moves above the upper endpoint, the edge becomes inactive.

ACTIVE EDGE TABLE Active edges are sorted according to increasing X. Filling in pixels between left most edge intersection and stops at the second. Restarts at the third intersection and stops at the fourth.