Transcript Lecture 9 - Scan Line Fill - College of Computer and Information
April 26, 2020
CS 4300 Computer Graphics
Prof. Harriet Fell Fall 2012 Lecture 9 – September 24, 2012 College of Computer and Information Science, Northeastern University 1
Today’s Topics
• Fill: Flood Boundary Fill vs. Polygon Fill See Photoshop for Flood Fill • 2D Polygon Fill April 26, 2020 College of Computer and Information Science, Northeastern University 2
Simple
2D Polygon
• an ordered sequence of line segments
g
0 ,
g
1 ,...,
g n
1
n
2 • such that each edge
g i
start vertex
s i
= (
s i
,
e i
) is the segment from a to an end vertex
e i e i-1
=
s i
for
0 < i ≤ n-1
and
e n-1
=
s 0
non-adjacent edges do not intersect the only intersection of adjacent edges is at their shared vertex 4/26/2020 College of Computer and Information Science, Northeastern University 3
Scan Line Polygon Fill
April 26, 2020 College of Computer and Information Science, Northeastern University 4
Parity Check
Draw a horizontal half-line from P to the right.
Count the number of times the half-line crosses an edge.
1 in 4 7 out in 7 in April 26, 2020 College of Computer and Information Science, Northeastern University 5
Polygon Data Structure
edges xmin ymax 1/m 1 6 8/4 (9, 6) xmin = x value at lowest y ymax = highest y Why 1/m?
(1, 2) If y = mx + b, x = (y-b)/m.
x at y+1 = (y+1-b)/m = (y-b)/m + 1/m.
April 26, 2020 College of Computer and Information Science, Northeastern University 6
13 12 11 10 9 8 7 6 5 4 3 2 1 0 e6 e4 e3 e2 e10 e5 e7 e1 e9 e8 e11
Polygon Data Structure
Edge Table (ET) has a list of edges for each scan line.
13 10 e3 e4 e5 e6 e7 e8 5 e9 0 e2 e1 e11 e10
Preprocessing the edges
For a closed polygon, there should be an even number of crossings at each scan line. We fill between each successive pair.
count twice, once for each edge delete horizontal edges April 26, 2020 chop lowest pixel to only count once College of Computer and Information Science, Northeastern University 8
13 12 e6 e6 9 8 e4 e5 e5 e8 5 4 3 2 1 0 e2 e10 e9 e11
Polygon Data Structure
after preprocessing Edge Table (ET) has a list of edges for each scan line.
13 10 e3 e4 e5 e6 e7 e8 5 e9 0 e2 e1 e11 e10
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. Merge y bucket from ET into SLB; sort on xmin.
b. Fill pixels between rounded pairs of x values in SLB.
c.
Remove edges from SLB whose ytop = current y.
d. Increment xmin by 1/m for edges in SLB.
e. Increment y by 1.
10 April 26, 2020 College of Computer and Information Science, Northeastern University
ET 13 12 11 10 9 8 7 2 5 4 3 6 1 0 e6 e3 e7 ve8 e4 e5 e2 e10 e11 e9 e2 e3 e4 e5 e6 e7 e8 xmin ymax 1/m 2 1/3 4 4 6 12 12 13 6 2/3 13 10 10 10 8 e9 11 e10 11 e11 6 8 4 4 -2/5 1/3 -2/5 0 -4/3 -1/2 2 3/8 -3/4 2/3
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15
y=0 SCB April 26, 2020 4 e10 -3/4 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 12
y=1 SLB April 26, 2020 6 e2 -2/5 4 e11 2/3 4 e10 -3/4 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 13
y=2 SLB April 26, 2020 6 e2 -2/5 4 e11 2/3 4 e10 -3/4 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 14
y=3 SLB April 26, 2020 6 e2 -2/5 4 e11 2/3 4 e10 -3/4 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 15
y=4 SLB 4/5 8 8 6 e2 -2/5 4 e11 2/3 4 e10 -3/4 12 4/8 8 e9 3/8 April 26, 2020
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 Remove these edges.
15 16
y=4 SLB 6 e2 -2/5 8 e9 3/8 e11 and e10 are removed.
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 April 26, 2020 17
y=5 SLB April 26, 2020 6 e2 -2/5 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 18
y=6 SLB 0 Remove this edge.
6 e2 -2/5 10 e7 -1/2 8 e8 2 8 e9 3/8
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15 April 26, 2020 19
1/3 4 12 e3 1/3 12 e4 -2/5 4 y=7 SLB 9 1/2 13 e5 0 10 e7 -1/2 12 8 e8 2 13 5/8 8 e9 3/8 Add these edges.
Running the Algorithm
13 10 e3 e4 e5 e6 e7 e8 5 e9 e2 e11 e10 0 0 5 10 15
Non-Simple Polygons
4/26/2020 College of Computer and Information Science, Northeastern University 21
Even-odd Rule
• construct a ray
r
in an arbitrary direction from the test point
p
• count number of intersections of
r
with the polygon.
p
is defined to be inside the poly if the intersection count is odd.
4/26/2020 College of Computer and Information Science, Northeastern University 22
Reasoning
• each time an edge is crossed, either switch from inside to outside or outside to inside • but since poly is closed, know that ray
r
must end up outside • this is the method we just applied 4/26/2020 College of Computer and Information Science, Northeastern University 23
Nonzero Rule
• • construct a ray
r
as before • compute all intersections of
r
with the poly edges
g i
, but this time keep track of whether the edge crossed from left to right (i.e.
s i
on left side of
r
and
e i
on right side of
r
) or right to left • count +1 for left-to-right and –1 for right-to-left
p
is defined to be inside the poly if and only if (iff) final count is nonzero 4/26/2020 College of Computer and Information Science, Northeastern University 24
Reasoning
• consider sweeping a point
q
along the perimeter of the poly • take the integral of the orientation angle of the line segment from
p
to
q
• turns out that, for one complete sweep of the polygon, the integrated
winding angle
will be an integer multiple of
cover it)
4/26/2020 College of Computer and Information Science, Northeastern University 25
Intuition
• intuition: reasonable definition of inside ness is to check if the poly “circled around”
p
in CCW direction different number times than in CW direction • nonzero intersection test turns out to be a shortcut to compute (proof is a little subtle and we will not cover it) 4/26/2020 College of Computer and Information Science, Northeastern University 26
The Results Can be Different
4/26/2020 College of Computer and Information Science, Northeastern University 27