Sweep Line Algorithm
Total Page:16
File Type:pdf, Size:1020Kb
Sweep Line Algorithm • Event C: Intersection point – Report the point. – Swap the two intersecting line segments in the sweep line status data structure. – For the new upper segment: test it against its predecessor for an intersection. – Insert intersection point (if found) into event queue. – Similar for new lower segment (with successor). • Complexity: – k such events – O(lg n) each – O(k lg n) total. Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 550 Example a3 b4 Sweep Line status: s3 e1 s4 s0, s1, s2, s3 Event Queue: a2 a4 b3 a4, b1, b2, b0, b3, b4 s2 b1 b0 s1 b2 s0 a1 a0 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 551 Example a3 b4 Actions: s3 Insert s4 to SLS e1 s4 Test s4-s3 and s4-s2. a2 a4 b3 Add e1 to EQ s2 b1 Sweep Line status: b0 s1 b2 s0, s1, s2, s4, s3 s0 a1 a0 Event Queue: b1, e1, b2, b0, b3, b4 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 552 Example a3 b4 Actions: s3 e1 s4 Delete s1 from SLS Test s0-s2. a2 a4 b3 Add e2 to EQ s2 Sweep Line status: e2 b0 b1 s1 b2 s0, s2, s4, s3 s0 a1 a0 Event Queue: e1, e2, b2, b0, b3, b4 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 553 Example a3 b4 Actions: s3 e1 s4 Swap s3 and s4 in SLS Test s3-s2. a2 a4 b3 Sweep Line status: s2 s0, s2, s3, s4 e2 b0 b1 s1 b2 Event Queue: s0 a1 a0 e2, b2, b0, b3, b4 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 554 Complexity Analysis • Total time complexity: – O((n+k)lgn). – Best case: k = O(1), then this is much better than the naive algorithm. – Even for k ≈ n, this is still the case. – Worst case: k ≈ n 2, then this is slightly worse than the naive algorithm. • Total space complexity: – Event queue: min-heap • O(n+k) – Sweep line status: red-black tree • O(n). – Total: O(n+k). Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 555 6.2 Convex Hull Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 556 Thursday March 12, 2015 557 ConvexConvex HullHull • The convex hull of a set of points is the smallest convex polygon containing the set of points. • A convex polygon is a non-self-intersecting polygon whose internal angles are all convex (i.e., less than 180 degrees) • In a convex polygon, a segment joining any two vertices lies entirely inside the polygon. segment not contained! convex nonconvex Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 557 Thursday March 12, 2015 558 ConvexConvex HullHull • Think of a rubber band snapping around the points • Remember to think of special cases – Collinearity: A point that lies on a segment of the convex is not included in the convex hull Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 558 Thursday March 12, 2015 559 ApplicationsApplications • Motion planning – Find an optimal route that avoids obstacles for a robot •Bounding Box – Obtain a closer bounding box in computer graphics • Pattern Matching – Compare two objects using their convex hulls obstacle Jacobs University end Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 559 Thursday March 12, 2015 560 FindingFinding aa ConvexConvex HullHull One algorithm for determining a convex polygon is one which, when following the points in counterclockwise order, always produces left-turns Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 560 Thursday March 12, 2015 561 CalculatingCalculating OrientationOrientation • The orientation of three points (a, b, c) in the b plane is clockwise, counterclockwise, or collinear c CW – Clockwise (CW): right turn – Counterclockwise (CCW): left turn a – Collinear (COLL): no turn c • The orientation of three points is characterized by the sign of the determinant ∆(a, b, c) b CCW xa ya 1 a Δ(a,b,c) = xb yb 1 c b xc yc 1 COLL a function isLeftTurn(a, b, c): return (b.x - a.x)*(c.y - a.y)– (b.y - a.y)*(c.x - a.x) > 0 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 561 Thursday March 12, 2015 562 ExamplesExamples Using the isLeftTurn() method: b (0.5,1) (0.5-0) * (0.5-0) - (1-0) * (1-0) c (1, 0.5) CW = -0.75 (CW) a (0,0) c (0.5,1) (1-0) * (1-0) - (0.5-0) * (0.5-0) b (1, 0.5) CCW = 0.75 (CCW) a (0,0) c(2,2) (1-0) * (2-0) - (1-0) * (2-0) b(1,1) = 0 (COLL) a(0,0) COLL Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 562 Thursday March 12, 2015 563 OrientationsOrientations andand convexconvex hullhull •Let a, b, c be three consecutive vertices of a polygon, in counterclockwise order – b’ is not included in the hull if a’, b,’ c’ is non-convex, i.e. orientation(a’, b’, c’) = CW or COLL – b is included in the hull if a, b, c is convex, i.e. orientation(a, b, c) = CCW, and all other non-hull points have been removed c’ c b’ a’ b a Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 563 Thursday March 12, 2015 564 GrahamGraham ScanScan AlgorithmAlgorithm • Find the anchor point (the point with the smallest y value) • Sort points in CCW order around the anchor • We can sort by increasing angle of the line segments from the anchor to the points with the x-axis 7 4 5 6 3 8 2 Anchor 1 Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 564 Thursday March 12, 2015 565 GrahamGraham ScanScan AlgorithmAlgorithm • The polygon is traversed in sorted order •A sequence H of vertices in the hull is maintained. •For each point a, add a to H. • While the last turn is a right turn, remove the second-to-last point from H – In the image below, p,q,r forms a right turn. –Thus q is removed from H. – Similarly, o,p,r forms a right turn. –Thus p is removed from H. r r o r o p p n q H H H Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 565 Thursday March 12, 2015 566 GrahamGraham ScanScan function graham_scan (pts): // Input: Set of points pts // Output: Hull of points find anchor point sort other points in CCW order around anchor hull = [] for p in pts: add p to hull while last turn is a “right turn” remove 2nd to last point add anchor to hull return hull Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 566 Thursday March 12, 2015 567 AnalysisAnalysis function graham_scan(pts): // Input: Set of points pts // Output: Hull of points find anchor point // O(n) sort other points in CCW order around anchor // O(n lg n) hull = [] // O(1) for p in pts: // O(n) add p to hull // O(1) while last turn is a “right turn” // O(1) // Each point is looked at, at most, twice: // 1x left-turn, 1x right-turn. remove 2nd to last point // O(1) add anchor to hull // O(1) return hull Overall run time: O(n lg n) Space complexity: O(n) [often a stack is used] Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 567 Thursday March 12, 2015 568 IncrementalIncremental AlgorithmAlgorithm • What if we already have a convex hull, and we just want to add one point q? • Get the angle from the anchor to q and find points p and r, the hull points on either side of q •If p,q,r forms a left turn, add q to the hull •Check if adding q creates a concave shape – If you have right turns on either side of q, remove vertices until the shape becomes convex – This is done in the same way as the static Graham Scan Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 568 Thursday March 12, 2015 569 IncrementalIncremental AlgorithmAlgorithm Original Hull: Want to add point q: Find p and r: q q p H H H r p, q, r forms a left o, p, q forms a right n, o, q forms a right turn, so add q: turn, so remove p turn, so remove o q q q n n n o o p H H H s s s r r r Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 569 Thursday March 12, 2015 570 IncrementalIncremental AlgorithmAlgorithm Since m, n, q is a left Now we look at the Since q, r, s is a left turn, we’re done with that other side: turn, we’re done! side. q q q n m H H H s s r r • Remember that you can have right turns on either or both sides, so make sure to check in both directions and remove concave points! Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 570 Thursday March 12, 2015 571 AnalysisAnalysis •Let n be the current size of the convex hull, stored in a binary search tree around the anchor for efficiency •To check if a point q should be in the hull, we insert it into the tree and get its neighbors (p and r on the prior slides): O(lg n) • We then traverse the ring, possibly deleting d points from the convex hull: O((1 + d) lg n) • Therefore, incremental insertion is O(d log n) where d is the number of points removed by the insertion Jacobs University Visualization and Computer Graphics Lab CH08-320201: Algorithms and Data Structures 571 A Divide-and-Conquer Approach • First, sort all points by their x coordinate.