Fill Algorithm
Fill Algorithm
Acknowledgements
Some of the material for the slides were adapted from College of Computer Information Science , Northeastern University Some of the images were taken from Hearn, Baker, and Carithers, Computer Graphics with OpenGL
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
Scan-line: simple objects, polygons, circles,.. Fill-method: complex objects, interactive fill.
b c
a,b,c and d are intersected by 2 line segments each. Count b,c twice but a and d once. Why?
a c b 2 2 2 2 b c d 1 2 d
Solution: Make a clockwise or counter-clockwise traversal on edges. Check if y is monotonically increasing or decreasing. If direction changes, double intersection, a otherwise single intersection. 1
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
y2 : : y4 : : y1 : : 0
y3
x2 1/m[2,3]
y5
x4 1/m[4,5]
y3
x4 1/m[4,3]
5 4
y5
x1 1/m[1,5]
y2
x1 1/m[1,2]
3 2
c 4 a 1
1/m[3,4] y2
b
xd 1/m[1,2]
y5
xa 1/m[1,5]
y2
xb 1/m[1,2]
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.)
e7 e8 e8
e2
e11 e1
The Algorithm
1. Start with smallest nonempty y value in ET. 2. Initialize SLB (Scan Line Bucket) to nil. 3. While current y top y value:
a. b. c. d. e. Merge y bucket from ET into SLB; sort on xmin. Fill pixels between rounded pairs of x values in SLB. Remove edges from SLB whose ytop = current y. Increment xmin by 1/m for edges in SLB. Increment y by 1.
ET
13 12
11
10 9 8
e6 e3 e4 e5 e7 e8 e2 e11 e10 e9 ymax 6 12 12 13 13 10 8 8 4 4 1/m -2/5 1/3 -2/5 0 -4/3 -1/2 2 3/8 -3/4 2/3
7 6
5 4 3 2
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
1 0
e2
0
e11
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
10
e2
0
e11
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
e2
0
e11
6/8 11 3/8
3/8
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
e2
0
e11
12 1/8 11 6/8
3/8
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
7 1/3 8 8 8 3/4
e2
0
e11
12 1/8 4/8
3/8
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
8 8
e2
0
e11
12 4/8
3/8
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
10
e2
e11
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
10
e2
0
e11
e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15
10
910 1/2
e2
0
e11
13 10
e3
e4
e5
e7
e8 e9
e2
0 0
e11 e10
5 10
15