Fill Algorithms: Soon Tee Teoh CS 116B
Fill Algorithms: Soon Tee Teoh CS 116B
Fill Algorithms
Given the edges defining a polygon, and a color for the polygon, we need to fill all the pixels inside the polygon. Three different algorithms:
1. Scan-line fill 2. Boundary fill 3. Flood fill
Advantage of scan-line fill: It does fill in the same order as rendering, and so can be pipelined.
out in out in
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
Scan-line
Case A
Case B
Let xk be the x intercept of the current scan line, and xk+1 be the x intercept of the next scan line, then
xk+1 = xk + Dx/Dy
Algorithm:
Suppose m = 7/3 Initially, set counter to 0, and increment to 3 (which is Dx). When move to next scan line, increment counter by increment When counter is equal or greater than 7 (which is Dy), increment the x-intercept (in other words, the x-intercept for this scan line is one more than the previous scan line), and decrement counter by 7.
yk xk
decrement
decrement
0 4 1 5 2 6 3 0 y0 x0
decrement
Each scan-line entry thus contains a sorted list of edges. The edges are sorted left to right. (To maintain sorted order, just use insertion based on their x value.)
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(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); } }
Flood Fill
Suppose we 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.
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); } }