Lecture filling algorithms


Published on

Published in: Technology, Art & Photos
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture filling algorithms

  1. 1. Polygon surfaces <ul><li>A polygon is an important graphics primitive. </li></ul><ul><li>A polygon is a closed area of image bounded by straight or curved lines and filled with one solid color. </li></ul><ul><li>Since images are two dimensional, a polygon is a closed planar figure. </li></ul>
  2. 2. Polygon surfaces <ul><li>A polygon can be defined as an image which consists of a finite ordered set of straight boundaries called edges . </li></ul><ul><li>The polygon can also be defined by an ordered sequence of vertices , i.e, the corners of the polygon. </li></ul><ul><li>The edges of the polygon are then obtained by traversing the vertices in the given order; </li></ul><ul><li>Two consecutive vertices define one edge. </li></ul><ul><li>The polygon can be closed by connecting the last vertex to the first. </li></ul><ul><li>Face list or polygon surface table is required in order to fill the polygon. </li></ul>
  3. 3. <ul><li>  </li></ul>
  4. 4. Area Filling Algorithms <ul><li>Scan line polygon fill algorithm </li></ul><ul><li>Boundary fill algorithm </li></ul><ul><li>Flood fill algorithm </li></ul>
  5. 5. Scan-Line Polygon Fill Algorithm <ul><li>Odd-parity rule </li></ul><ul><li>Calculate span intersections on each scan line using parity rule to determine whether or not a point is inside a region </li></ul><ul><li>Parity is initially even </li></ul><ul><li> each intersection encountered thus inverts the parity bit </li></ul><ul><li>parity is odd  interior point (draw) </li></ul><ul><li>parity is even  exterior point (do not draw) </li></ul>o o e e o e/o e
  6. 6. Scan-Line Polygon Fill Algorithm <ul><li>Case 1 </li></ul><ul><li>Check the endpoint y values of two intersected edges </li></ul><ul><li> if their change is monotonically, we should count the intersection only once </li></ul><ul><li> Otherwise, we should count the intersection twice. </li></ul><ul><li>Case 2 </li></ul><ul><li>At the shared vertex: shorten the lower edge one grid below the shared point, such that the two edges are disconnected; </li></ul><ul><li> Alternative way: we only count the y[min] vertex of an edge but not the y[max] vertex for the adjacent edge. </li></ul><ul><li>For horizontal line </li></ul><ul><li> do not count their vertices </li></ul>o o e e o e/o e Case 1: Case 2:
  7. 7. Scan-Line Polygon Fill Algorithm <ul><li>Finding intersection pairs : </li></ul><ul><li>Scan-line conversion method (for non-horizontal edges): </li></ul><ul><li> Edge coherence property: </li></ul><ul><li>Incremental calculation between successive scan lines </li></ul><ul><li> Or using Bresenham’s scan line conversion algorithm on each edge and keep a table of span extrema for each scan line </li></ul>
  8. 8. Scan-Line Polygon Fill Algorithm <ul><li>Incremental scan line method : </li></ul><ul><li>m = (y[k+1] – y[k]) / (x[k+1] – x[k]) </li></ul><ul><li>y[k+1] – y[k] = 1 </li></ul><ul><li>x[k+1] = x[k] + 1/m  x[k] = x[0] + k/m </li></ul>Scan line y[k] + 1 Scan line y[k] (x[k+1], y[k+1]) (x[k], y[k])
  9. 9. Scan-Line Polygon Fill Algorithm <ul><li>Incremental scan line method : </li></ul><ul><li>Bucket sorted edge table: containing all edges sorted by their smaller y coordinate. </li></ul><ul><li>Each bucket: edges are recorded in order of increasing x coordinate of the </li></ul><ul><li>lower endpoint. </li></ul><ul><li>Each entry of a bucket: y[max] of the edge, x[min] of lower endpoint and 1/m </li></ul><ul><li>(x increment) </li></ul><ul><li>Active-edge table (or list): keep track of the set of edges that the scan line intersects </li></ul><ul><li>and the intersection points in a data structure </li></ul>
  10. 10. Scan-Line Polygon Fill Algorithm <ul><li>Example: </li></ul>5 1 4 3 2 y5 x1 1/m[1,5] y2 x1 1/m[1,2] y5 x4 1/m[4,5] y3 x4 1/m[4,3] y3 x2 1/m[2,3] y4 : : y1 y2 : : : : 0 Sorted edge table (ET)
  11. 11. Scan-Line Polygon Fill Algorithm <ul><li>Example: </li></ul>5 1 4 3 2 y5 xa 1/m[1,5] y2 xb 1/m[1,2] y5 x5 1/m[1,5] y5 x5 1/m[4,5] y[5] : : y[a] : : : : : 0 Active edge table (AET) b a y3 xc 1/m[3,4] y2 xd 1/m[1,2] c d
  12. 12. Inside-outside tests <ul><li>Odd-even rule (odd parity rule or even-odd rule) </li></ul><ul><li>Counting the number of edge crossing along the line from point P to infinity </li></ul><ul><li>Odd number  interior point P </li></ul><ul><li>Even number  exterior point P </li></ul><ul><li>Non-zero winding number rule (vector method) </li></ul><ul><li> winding number: no of times that the polygon edges wind around a point in the counterclockwise direction </li></ul><ul><li> if the winding number = 0  exterior point </li></ul><ul><li> if the winding number != 0  interior point </li></ul>
  13. 13. Boundary Fill <ul><li>Suppose that the edges of the polygon has already been colored. </li></ul><ul><li>Suppose that the interior of the polygon is to be colored a different color from the edge. </li></ul><ul><li>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. </li></ul>
  14. 14. <ul><li>void boundaryFill(int x, int y, int fillColor, int borderColor) { </li></ul><ul><li>int interiorColor; </li></ul><ul><li>getPixel(x,y,interiorColor); </li></ul><ul><li>if ((interiorColor!=borderColor)&&(interiorColor!=fillColor)) { </li></ul><ul><li>setPixel(x,y,fillColor); </li></ul><ul><li>boundaryFill(x+1,y,fillColor,borderColor); </li></ul><ul><li>boundaryFill(x-1,y,fillColor,borderColor); </li></ul><ul><li>boundaryFill(x,y+1,fillColor,borderColor); </li></ul><ul><li>boundaryFill(x,y-1,fillColor,borderColor); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Boundary Fill
  15. 15. Flood Fill <ul><li>Suppose we want to color the entire area whose original color is interiorColor, and replace it with fillColor. </li></ul><ul><li>Then, we start with a point in this area, and then color all surrounding points until we see a pixel that is not interiorColor. </li></ul>
  16. 16. <ul><li>void floodFill(int x, int y, int fillColor, int interiorColor) { </li></ul><ul><li>int color; </li></ul><ul><li>getPixel(x,y,color) </li></ul><ul><li>if (color==interiorColor) { </li></ul><ul><li>setPixel(x,y,fillColor); </li></ul><ul><li>floodFill(x+1,y,fillColor,interiorColor); </li></ul><ul><li>floodFill(x-1,y,fillColor,interiorColor); </li></ul><ul><li>floodFill(x,y+1,fillColor,interiorColor); </li></ul><ul><li>floodFill(x,y-1,fillColor,interiorColor); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Flood Fill