Computational Geometry Part 2: Line Sweep, Convex Hulls Lucca Siaudzionis and Jack Spalding-Jamieson 2020/02/27 University of British Columbia
Announcements • A3 is due Sundayyyyy! • Project topics are due Tuesday! • Presentation dates will be on March 31st and April 2nd . • If you can’t present in one of those two days, let us know by March 4th so we can schedule you on the other (with a decent reason). • Otherwise, dates will be randomly assigned. • Extra office hours tomorrow from 2-4PM in ICCSX237. 1
Discussion Problem: Remember How to do Line Sweep? Jack is constantly busy with meetings that take up all his time. Many of his meetings even overlap! Given a list of 1 ≤ n ≤ 10 6 meetings along with their start and end times, find out how much time Jack spends overwhelmed with meetings. 2
Discussion Problem: Remember How to do Line Sweep? – Insight We can process the “events” (start and end times) in increasing order (the “ x ”-coordinate in Line Sweep), keeping track of how many intervals are currently under the sweep line. 3
Line sweep: General Plan The general method for solving sweep problems: • Identify important “events” that occur as we move from left to right. • Keep track of some auxiliary data structure that lets us • find the answer, and • update efficiently at each event. 4
Line Sweep: Pachinko Given N ≤ 10 5 non-intersecting, non-horizontal line segment obstacles, where does the pinball end up? 5
Line Sweep: Pachinko We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right. 6
Line Sweep: Pachinko We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right. 7
Line Sweep: Pachinko We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from left to right. 8
Line Sweep: Pachinko Data structure: A set of lines, sorted by their order on the y -axis. Events: The line segment endpoints. Actions: • Left endpoint: Insert line into the set • Right endpoint: Remove line from the set • Lower endpoint of segment: Query for the next line below the point 9
Convex Sets A set S is convex ⇔ ∀ x , y ∈ S , 0 ≤ λ ≤ 1 , λ x + (1 − λ ) y ∈ S ⇔ the line segment x → y is inside the set Figure 1: In a convex set, the line segment between any 2 points lies in a set 10
Convex Hulls: Definition A convex hull of a set is the smallest convex set containing the set. Figure 2: Some sets and their convex hulls Why do we care about convex hulls? They give us nice convex polygons, which we can optimize over easier. 11
Convex Hulls: Finite Point Sets If we have a finite set of points, we define the following about a point p and the convex hull H : • p may be in the interior, i.e. p is not found on H at all. • p may be a vertex of the convex hull, i.e. removing p would make the convex hull smaller. • p could lie on an edge in the convex hull. When finding a convex hull, we need to know which of these sets we’re looking for (just the vertices? or all the points on the convex hull). 12
Convex Hulls: Applications Convex Hulls can be used for many real-world applications, such as: • In game programming, they can be used to speed up collision-detection, similarly to bounding-boxes. • Can be used to find the shortest paths around physical objects (remember the circle problem from last class?). • And much more... 13
Convex Hulls: Gift Wrapping How do we find the convex hull of a finite point-set with n points? 14
Convex Hulls: Gift Wrapping How do we find the convex hull of a finite point-set with n points? We can use the gift wrapping algorithm : • Start at some point that is guaranteed to be a vertex in the convex hull, e.g. the leftmost point (break ties by height). • Look at all of the other points, and choose the one that maximizes the angle with the line segment pointing downwards from our original point. • The point chosen is the next point in the convex hull. • Do this iteratively until we hit a point already added, but with the most recent line segment added to the convex hull. Overall runtime: O ( n 2 ) 14
Convex Hulls: Gift Wrapping Example (1) 15
Convex Hulls: Gift Wrapping Example (2) 16
Convex Hulls: Gift Wrapping Example (3) 17
Convex Hulls: Gift Wrapping Example (4) 18
Convex Hulls: Gift Wrapping Example (5) 19
Convex Hulls: Gift Wrapping Example (6) 20
Convex Hulls: Gift Wrapping Example (7) 21
Convex Hulls: Gift Wrapping Example (8) 22
Convex Hulls: Gift Wrapping Example (9) 23
Convex Hulls: Gift Wrapping Example (10) 24
Convex Hulls (Aside): Gift Wrapping – Output Sensitivity Although the runtime of gift wrapping is in fact a tight Θ( n 2 ), we can actually give a better bound! This is using something called output sensitivity : If we know that there will be at most h < n points on the eventual convex hull, we can actually bound the algorithm with complexity O ( nh ). 25
Convex Hulls: Faster Algorithms Are there faster algorithms for solving this? 26
Convex Hulls: Faster Algorithms Are there faster algorithms for solving this? Yes. Lots. No less than eight: • Graham Scan ( O ( n log n )) • Monotone Chain/Andrew’s Algorithm ( O ( n log n )) • Quickhull ( O ( n log n ) average case) • Divide & Conquer via axis split ( O ( n log n )), extendable to 3D! • Divide & Conquer with arbitrary merge and rotating calipers ( O ( n log n )) • Kallay’s incremental algorithm ( O ( n log n )) • Kirkpatrick-Seidel Algorithm ( O ( n log h )), aka the ”ultimate planar convex hull algorithm” • Chan’s Algorithm ( O ( n log h )) We’re going to talk about Monotone Chain/Andrew’s Algorithm, although the algorithm is very similar to Graham’s algorithm. 26
Monotone Chain Algorithm Step 1: scan from left to right, “wrap” around the top Step 2: scan from right to left, “wrap” around the bottom. Figure 3: Upper and lower hulls built by monotone chain algorithm 27
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm 28
Monotone Chain Algorithm Algorithm summary • Sort all points by x -coordinate • Start from leftmost point (i.e. smallest x -coordinate) • If there are ties, you can take the uppermost one (largest y -coordinate) • Iteratively add points into the hull • If adding a point causes CCW turn, pop points from hull until this is no longer the case before adding! • Repeat the above steps from right to left. 29
Monotone Chain Algorithm Edge cases • Convex hull of 1, 2, 3 points • Collinear points 30
Monotone Chain Algorithm Time complexity analysis • Sort all points by x -coordinate: O ( n log n ) • Each point is added and removed at most once: O ( n ) ⇒ Time complexity: O ( n log n ) Without looking at output sensitivity, can we do better? 31
Monotone Chain Algorithm Time complexity analysis • Sort all points by x -coordinate: O ( n log n ) • Each point is added and removed at most once: O ( n ) ⇒ Time complexity: O ( n log n ) Without looking at output sensitivity, can we do better? With angle-based comparison, we can use the sorting lower bound: Arrange n points on a circle ⇒ equivalent to sorting in some sense, Ω( n log n ). 31
Discussion Problem: Visibility Input : A point-set D of 1 ≤ n ≤ 10 5 key-points representing some convex geometric object, guaranteed to include all boundary points. Additionally, there are 1 ≤ q ≤ 10 5 queries. For each query, a point p is provided, guaranteed to be outside the boundary of the object. Output : For each query, output the angles over which a camera positioned at point p would not be able to see past the object. P 32
Discussion Problem: Visibility – Insight (1) Observation 1 : The solution for each query lies on the convex hull. Observation 2 : Pick an arbitrary point on the hull, draw a line through, then one extremum is on each side of the line. P 33
Discussion Problem: Visibility – Insight (2) Binary search to find start/end vertices on each half of the hull, and then binary search on each half of the hull for when P → p i → p i +1 changes from CCW to CW (or vice versa). Time complexity: O ( N log N + Q log N ) This problem has many applications, including game programming, graphics, and more broad computational geometry problems (such as euclidean shortest path problems). 34
Recommend
More recommend