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