CS 4300 Computer Graphics Prof. Harriet Fell Fall 2012 Lecture 9 – September 24, 2012 April 8, 2015 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 8, 2015 College of Computer and Information Science, Northeastern University 2 Simple 2D Polygon • an ordered sequence of line segments g , g ,..., g n 2 0 1 n1 • such that each edge gi = (si, ei) is the segment from a start vertex si to an end vertex ei ei-1 = si for 0 < i ≤ n-1 and en-1 = s0 non-adjacent edges do not intersect the only intersection of adjacent edges is at their shared vertex 4/8/2015 College of Computer and Information Science, Northeastern University 3 Scan Line Polygon Fill April 8, 2015 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. April 8, 2015 1 in 4 7 out in 7 in College of Computer and Information Science, Northeastern University 5 Polygon Data Structure edges xmin ymax 1/m (9, 6) 1 6 xmin = x value at lowest y 8/4 (1, 2) ymax = highest y Why 1/m? If y = mx + b, x = (y-b)/m. x at y+1 = (y+1-b)/m = (y-b)/m + 1/m. April 8, 2015 College of Computer and Information Science, Northeastern University 6 Polygon Data Structure 13 12 11 10 e6 Edge Table (ET) has a list of edges for each scan line. 9 8 7 e4 e5 6 e3 e7 13 e8 5 e6 10 e3 4 3 e5 e4 e7 e8 5 2 1 e2 e1 0 e10 e9 e2 e11 0 e9 e11 e1 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 April 8, 2015 delete horizontal edges chop lowest pixel to only count once College of Computer and Information Science, Northeastern University 8 Polygon Data Structure 13 12 after preprocessing 11 e6 10 e6 Edge Table (ET) has a list of edges for each scan line. 9 8 7 e3 e4 e4 e5 e5 6 e7 e3 e8 e7 e8 5 13 e6 10 e3 4 3 e5 e4 e7 e8 5 2 1 e2 0 e10 e9 e2 e1 e11 e11 0 e9 e11 e1 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. b. c. d. e. April 8, 2015 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. College of Computer and Information Science, Northeastern University 10 ET Running the Algorithm 13 12 11 e6 10 9 8 7 6 e3 e4 e5 e7 ve8 5 4 3 2 1 0 e2 e11 e10 e9 xmin e2 2 e3 1/3 e4 4 e5 4 e6 6 2/3 e7 10 e8 10 e9 11 e10 11 e11 6 ymax 6 12 12 13 13 10 8 8 4 4 13 e6 10 e3 1/m -2/5 1/3 -2/5 0 -4/3 -1/2 2 3/8 -3/4 2/3 e5 e4 e7 e8 5 e2 e9 e11 e10 0 0 5 10 15 Running the Algorithm y=0 SCB 1011 1/4 e10 4 -3/4 13 e6 10 e3 e9 1111 3/8 8 3/8 e5 e4 e7 e8 5 e2 e11 e10 0 0 April 8, 2015 e9 5 10 15 12 Running the Algorithm y=1 SLB 1 3/5 2 e2 6 -2/5 13 e6 10 6 2/3 6 e11 4 2/3 e3 e5 e4 e7 e8 5 10 9 1/2 1/4 e10 4 -3/4 e2 e11 e10 0 6/8 11 3/8 April 8, 2015 e9 8 3/8 e9 0 5 10 15 13 Running the Algorithm y=2 SLB 1 3/5 1/5 e2 6 -2/5 13 e6 10 7 6 1/3 2/3 e11 4 2/3 e3 e5 e4 e7 e8 5 8 1/2 3/4 9 e10 4 -3/4 e2 e11 e10 0 12 1/8 11 6/8 April 8, 2015 e9 8 3/8 e9 0 5 10 15 14 Running the Algorithm y=3 SLB 14/5 1/5 e2 6 -2/5 13 e6 10 78 1/3 e11 4 2/3 e3 e5 e4 e7 e8 5 8 8 3/4 e10 4 -3/4 e2 e11 e10 0 1/8 12 4/8 April 8, 2015 e9 8 3/8 e9 0 5 10 15 15 Running the Algorithm y=4 SLB 4/5 e2 6 -2/5 13 e6 10 8 e11 4 2/3 e3 e5 e4 e7 e8 5 8 e10 4 -3/4 e2 e11 e10 0 12 4/8 April 8, 2015 e9 8 3/8 e9 0 5 10 15 Remove these edges. 16 Running the Algorithm y=4 SLB 4/5 2/5 e2 6 -2/5 13 e6 10 12 7/8 4/8 e9 8 3/8 e3 e5 e4 e7 e8 5 e2 e11 and e10 are removed. e11 e10 0 0 April 8, 2015 e9 5 10 15 17 Running the Algorithm y=5 SLB 2/5 0 e2 6 -2/5 13 e6 10 12 2/8 7/8 13 e9 8 3/8 e3 e5 e4 e7 e8 5 e2 e11 e10 0 0 April 8, 2015 e9 5 10 15 18 Running the Algorithm Remove this edge. y=6 SLB 0 e2 6 -2/5 13 e6 10 910 1/2 e7 10 -1/2 e3 e5 e4 e7 e8 5 e8 12 10 8 2 e2 e11 e10 0 13 2/8 5/8 April 8, 2015 e9 8 3/8 e9 0 5 10 15 19 Add these edges. 1/3 e3 12 1/3 4 e4 12 -2/5 4 y=7 SLB 9 1/2 e5 13 0 e7 10 -1/2 8 2 13 e6 10 e3 5 0 e8 12 Running the Algorithm e2 13 5/8 e4 e7 e8 e9 e11 e10 0 e9 8 3/8 e5 5 10 15 Non-Simple Polygons 4/8/2015 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/8/2015 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/8/2015 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 gi, but this time keep track of whether the edge crossed from left to right (i.e. si on left side of r and ei 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/8/2015 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/8/2015 College of Computer and Information Science, Northeastern University 25 Intuition • intuition: reasonable definition of insideness 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/8/2015 College of Computer and Information Science, Northeastern University 26 The Results Can be Different 4/8/2015 College of Computer and Information Science, Northeastern University 27