You are on page 1of 13
COMPUTER GRAPHICS SOLID AREA (POLYGON) FILLING Institute of Information and Communication Technology University of Sindh, Jamshoro BY:OR.ZEESHANGHATTI Dr. Zeeshan Bhatti on PIV POLYGON ‘A polygon is a chain of connected line segments Itis specified by points called Vertices (aka nodes), usually denoted by P,, P,, P.,......P, ‘The first vertex is called the initial or starting point and the last vertex is called the final or terminal point. Asual POLYGON Representation: Polygon = Vo, V;, Vo, -- Va V.=(X,y,) - vertex E=(v,,v+1) - edge V2 vertex E,=(Vn.Vo) E EB Vo ne, E. V3, edge Vi Ve z FILL POLYGON Question: How do we know if a given point is inside or outside a polygon? INSIDE — OUTSIDE TEST To Scan Convert a polygon means to paint the interior of the polygon shape. To fill the polygon shape with solid color. How to differentiate between the interior and the exterior of a polygon. Two techniques |. Odd - Even Method 2. Winding Number Method oN ODD -EVENMI +: This is a very simple method. The steps of the method are as follows: Step 1: Select an outside point Step 2: Draw a horizontal straight line from the outside point to the point in question. Step 3: Count the number of intersections of the straight line with the polygon boundaries. Step 4: if the count is odd, the point in question is an inside point, otherwise it is an outside point. | WINDING NUMBER elt MET! number of times the polygon winds around the point P. The point is outside only when this "winding number" wn = 0; otherwise, the point is inside. “Add 1 (+1) to the winding number every time we intersect the edge that cross lines from right to left and subtract 1 (-1) form the winding number, every time we intersect cross lines from left to right. /\ «If the winding number is not equal to zero, ¢ the point P is within the polygon (inside), otherwise outside the polygon. Winding Nunber=0-1+1¢1=1=> Point Pis inthe interior 1. 2. WINDING NUMBER METHOD - : ALGORITHM torrsacinae The method of the non-zero winding NUiwwer aie wie Comes the number of times edges wind around a point P in a counter- clockwise direction. Initialize the winding number to zero. Draw a line from P to the exterior of the polygon without passing through a vertex. Add 1 to the winding number for every edge that crosses the line from P in one direction (clockwise or counter- clockwise) and subtract 1 from the winding number for every crossing edge in the opposite direction. If the winding number is not equal to zero, the point P is within the polygon. SCAN CONVERSION - DOI VGON Fil | Problem: Given a closed 2D polygon, fill its interior with a specified color, on a graphics display ai ®> @ Assumption: Polygon is simple, i.e. no self intersections, and simply connected, i.e. without holes Solutions: +Flood fill «Scan Conversion Boundary fill * Suppose the boundary is uniquely defined pixel quality. * A boundary fill algorithm starts from an interior point and expands until we reach the boundary. * The prole is to make sure that we really cover all of the interior! Boundary fill + Use stencils to test for additional interior points. + 4-connected stencil + 8-connected stencil Boundary-Fill Algorithm eo Figure 3-41 , Interior fill of an elliptical arc CY Fill methods applied toa 4-connected area (a) and to an S-connected area (b). Open circles represent pixels to be tested from the current test position, shown as a solid color 4 Figure 3-42 Example color boundaries for a boundary-fill procedure Start Position (a tb) Figure 3-44 The area defined within the color boundary (a) is only partially filled in (b) using a 4-connected boundary-fill algorithm. Boundary Fill Suppose that the edges of the polygon has already been colored. Suppose that the interior of the polygon is to be colored a different color from the edge. Suppose we start with a pixel inside the polygon, then we color that pixel and all surrounding pixels until we meet a pixel that is already colored. void boundaryFill(int x, int y, int fillColor, int borderColor) { int interiorColor; getPixel(xy,interiorColor); if ((interiorColor!=borderColor)8&.&(interiorColor!=fillColor)) { setPixel(x,y,fillColor); boundaryFill(x+1,y,fillColor,borderColor); boundaryFill(x-1,y,fillColor, borderColor); boundaryFill(x,y+1 fllColor,borderColor); boundaryFill(x,y-1 fillColor, borderColor); FLOOD FILL ALGORITHM Let P be a polygon with n vertices, vo .. v4 *Denote v,=V9 Let c be a color to paint P Let p=(x,y) be a point in P FLOOD FILL ALGORITHM FloodFill(P,x,y,¢) if (OnBoundary(x,y,P) or Colored (x,y,c)) then return; else begin PlotPixel(x,y.c); FloodFill(P,x+1,y,c); FloodFill(P,x,y+1,c); FloodFill(P,x,y-1,¢); FloodFill(P,x-1,y,c) end; Slow algorithm due to recursion, needs initial point SCAN CONVERSION — RASIC Al GORITHM +Let P be a polygon with n vertices, vg .. V,., *Denote v,=Vo eLet c be a color to paint P ScanConvert (P,c) Forj:=0 to ScreenYMax do 1 := points of intersection of edges from P with line y=j; Sort J in increasing x order and fill with color c alternating segments; end; Question: How do we find the intersecting edges? | FLOOD FILL VS. SCAN CONVERSION Flood Fill Scan Conversion “Very simple. *More complex *Requires a seed point *No seed point is required *Requires large stack size *Requires small stack size *Common in paint *Used in image rendering packages SCAN CONVERSION — FILL PAI VGON SCAN CONVERSION -— FILL POLYGON Intersections at pixel coordinates A S ! B E H © D F Rule: In the odd/even count, we count y,,,, vertices of an edge, but not Ynax vertices Vertex B is counted once because y,,, of (A,B) Vertex E is not counted because y,,,, Of both (D, E) and (E, F) Vertex H is counted twice because y,,;, of both (G, H) and (H, I) FILL POLYGON — OPTIMIZED ALGORITHM Uses a list of “active” edges A (edges currently intersecting the scan line) ScanConvert(P.c) Sort all edges E={E} in increasing MinY(E, ) order. A=D; For k :=0 to ScreenYMax do Foreach E,

You might also like