0% found this document useful (0 votes)
113 views31 pages

Fill Algorithm

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.

Uploaded by

ponga_pundit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views31 pages

Fill Algorithm

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.

Uploaded by

ponga_pundit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

SCAN-LINE FILL ALGORITHMS

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

Filled Area Primitives


Two basic approaches to area filling on raster systems:
Determine the overlap intervals for scan lines that cross the area (scan-line) Start from an interior position and point outward from this point until the boundary condition reached (fill method)

Scan-line: simple objects, polygons, circles,.. Fill-method: complex objects, interactive fill.

Scan-line Polygon Fill


For each scan-line: Locate the intersection of the scan-line with the edges (y=ys) Sort the intersection points from left to right. Draw the interiors intersection points pairwise. (a-b), (c-d) Problem with corners. Same point counted twice or not?

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

Scan Line Fill Algorithm


Basic algorithm:
Assume scan line start from the left and is outside the polygon. When intersect an edge of polygon, start to color each pixel (because now were inside the polygon), when intersect another edge, stop coloring Odd number of edges: inside Even number of edges: outside

Advantage of scan-line fill: It does fill in the same order as rendering, and so can be pipelined.

out in out in

Scan-Line Polygon Fill Algorithm


Odd-parity rule
Calculate span extrema (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 o e e

Scan Line Fill: What happens at edge end-point?


Edge endpoint is duplicated. In other words, when a scan line intersects an edge endpoint, it intersects two edges. Two cases:
Case A: edges are monotonically increasing or decreasing Case B: edges reverse direction at endpoint

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

Speeding up Scan-Line Algorithm


1. Parallel algorithm: process each scan line in one processor. Each scan line is independent 2. From edge intersection with one scan line, derive edge intersection with next scan line 3. Active edge list

Scan-Line Polygon Fill Algorithm


Finding intersection pairs:
Scan-line conversion method (for non-horizontal edges):

Taking advantage of the edge coherence property:


Incremental calculation between successive scan lines Or using Bresenhams or mid-point scan line conversion algorithm on each edge and keep a table of span extrema for each scan line

Scan-Line Polygon Fill Algorithm


Incremental scan line method:
m = (y[k+1] y[k]) / (x[k+1] x[k]) y[k+1] y[k] = 1 x[k+1] = x[k] + 1/m x[k] = x[0] + k/m (x[k+1], y[k+1]) (x[k], y[k]) Scan line y[k] + 1 Scan line y[k]

Derive next intersection


Suppose that slope of the edge is
m = Dy/Dx

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.

Line with slope 7/3

yk xk

decrement

decrement

0 4 1 5 2 6 3 0 y0 x0

decrement

Scan-Line Polygon Fill Algorithm


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 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:
3 2

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]

Sorted edge table (ET)

Scan-Line Polygon Fill Algorithm


Example:
5
: : : y[5] : : y[a] : : 0 y5 x5 1/m[1,5] y5 x5 1/m[4,5] y3 xc

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]

Active edge table (AET)

Active Edge List


Start with the sorted edge table.
In this table, there is an entry for each scan-line. Add only the non-horizontal edges into the table.
For each edge, we add it to the scan-line that it begins with (that is, the scan-line equal to its lowest y-value). For each edge entry, store (1) the x-intercept with the scan-line, (2) the largest yvalue of the edge, and (3) the inverse of the slope.

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.)

Next, we process the scan lines from bottom to top.


We maintain an active edge list for the current scan-line. The active edge list contains all the edges crossed by that scan line. As we move up, update the active edge list by the sorted edge table if necessary. Use iterative coherence calculations to obtain edge intersections quickly.

13 12 11 9 8 7 6 5 4 3 2 1 0 e2 e1 e11 e11 0 e10 e9 5 e4 e3 e3 e7 e5 e4 e5 13 10 e6 10 e6

Polygon Data Structure


after preprocessing
Edge Table (ET) has a list of edges for each scan line. e6 e3 e4 e5 e7 e8 e9 e10

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

Running the Algorithm


13 10

7 6
5 4 3 2

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

1 0

xmin e2 2 e3 1/3 e4 4 e5 4 e6 6 2/3 e7 10 e8 10 e9 11 e10 11 e11 6

e2
0

e11

Running the Algorithm


y=0 SCB 1011 1/4 e10 4 -3/4 e9 1111 3/8 8 3/8
5 13

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

10

e2
0

e11

Running the Algorithm


y=1 SLB 1 3/5 2 6 e2 -2/5
13 10

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

6 2/3 6 101/2 9 1/4

e11 4 2/3 e10 4 -3/4 e9

e2
0

e11

6/8 11 3/8

3/8

Running the Algorithm


y=2 SLB 1/5 1 3/5 6 e2 -2/5
13 10

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

7 1/3 6 2/3 8 3/4 9 1/2

e11 4 2/3 e10 4 -3/4 e9

e2
0

e11

12 1/8 11 6/8

3/8

Running the Algorithm


y=3 SLB 14/5 1/5 6 e2 -2/5
13 10

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

7 1/3 8 8 8 3/4

e11 4 2/3 e10 4 -3/4 e9

e2
0

e11

12 1/8 4/8

3/8

Running the Algorithm


y=4 SLB 4/5 6 e2 -2/5
13 10

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

8 8

e11 4 2/3 e10 4 -3/4 e9

e2
0

e11

12 4/8

3/8

Remove these edges.

Running the Algorithm


y=4 SLB 4/5 2/5 6 e2 -2/5 e9 12 7/8 4/8 8 3/8
5 13

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

10

e11 and e10 are removed.


0

e2

e11

Running the Algorithm


y=5 SLB 2/5 0 6 e2 -2/5 e9 12 7/8 13 2/8 8 3/8
5 13

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

10

e2
0

e11

Remove this edge. y=6 SLB 0 6

Running the Algorithm


13

e2 -2/5 e7 10 -1/2 e8 12 10 8 e9 13 2/8 5/8 8 3/8 2

e6 e3 e4 e5 e7 e8 e9 e10
0 5 10 15

10

910 1/2

e2
0

e11

e3 1/3 4 12 e4 12 e5 4 y=7 SLB 9 1/2 12 13 0 e7 10 -1/2 e8 8 e9 13 5/8 8 3/8 2 -2/5 1/3

Add these edges.

Running the Algorithm


e6

13 10

e3

e4

e5

e7

e8 e9

e2
0 0

e11 e10
5 10

15

You might also like