Lecture 17: Rotating calipers Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-19 University of British Columbia CPSC 490: Problem Solving in Computer Science
• Select a presentation topic by Friday, March 22. • Assignment 5 is due tonight at 23:59. 1 Announcements
inside or outside the polygon. Given a simple polygon (not necessarily convex) and a point, determine whether it lies 2 Warm-up – Point in polygon Figure 1: Points inside and outside
polygon an odd number of times. Watch out for: point on boundary, ray intersecting a vertex 3 Warm-up – Solution 1 Draw a ray from the point out to infinity. The point is inside ⇔ the ray intersects the
Walk around the polygon and compute the total angle subtended by each side of the polygon. 4 Warm-up – Solution 2 This total angle will be a multiple of 2 π . The point is inside ⇔ the total angle is nonzero.
Given a convex polygon, output the polygon obtained by cutting ofg everything to the leħt b . 5 Warm-up 2 – Cut polygon a → ⃗ of the line ⃗ Figure 2: Cutting a polygon
Walk around the polygon to find the cut points (or binary search). Since the polygon is convex, it is cut at most twice. Watch out for the line intersecting the polygon in a single point, or not at all. 6 Warm-up 2 – Solution
Find the two points that are farthest apart in Euclidean distance. 7 Problem 1 – Convex hull again Figure 3: Finding the pair of points that are farthest apart
Observation: answer is on the convex hull. What about trying every possible end point and ternary search for the other point? No! Adjacent points on hull could have same distance from your start point. 8 Problem 1 – Possible solutions Brute force: try all pairs of points on convex hull, O ( n 2 )
Observation: answer is on the convex hull. What about trying every possible end point and ternary search for the other point? No! Adjacent points on hull could have same distance from your start point. 8 Problem 1 – Possible solutions Brute force: try all pairs of points on convex hull, O ( n 2 )
Simple “2-pointer” greedy: 1. Initialize 2 pointers on two adjacent points on hull 2. Advance the second pointer as far as possible while distance increases 3. Advance the first pointer once and go back to 2 4. End once the first pointer has traveled full circle around hull Why does this work? When pointer 1 advances, can use geometry to prove that it is never optimal to move pointer 2 back. 9 Problem 1 – The greedy solution Time complexity: O ( n ) aħter getting convex hull
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
10 Rotating calipers
Find the smallest bounding box of these points. Rectangle could be rotated. 11 Problem 2 – Bounding box Figure 4: A possible (rectangular) bounding box
Observation: box must line up with one edge on the hull. Suppose not, then we can wiggle the rectangle without changing which vertices it touch. Now, how does the area change? so optimum is at extrema of movement. 12 Problem 2 – Solution Notice rectangle vertex moves on a semi-circle ⇒ “extra” area is concave-down function Figure 5: Rectangle vertices move along semicircles of convex hull chords
How to iterate through all rectangles quickly? Two pairs of calipers! Algorithm: initialize 4 perpendicular calipers, then rotate them, events are when any caliper hits an edge. Compute rectangle area at every event and take the min. 13 Problem 2 – Solution Time complexity: every caliper has at most n events, so O ( n ) aħter convex hull. Figure 6: Iterating through bounding boxes
general case? We learned how to merge two convex hulls separated by dividing line. What about the 14 Problem 3 – Merging convex hulls, again Figure 7: Merging two convex hulls
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when • Calipers switch relative order • Outer caliper hits an edge 15 Problem 3 – Solution Time complexity: O ( n )
Which point should we add to the convex hull to make it biggest? 16 Problem 4 – Non-parallel calipers Figure 8: Picking a point to enlarge convex hull
Observation: point must be on “big hull” of all points. • Pick arbitrary starting point on big hull, draw two tangent lines to small hull as initial calipers • Rotate calipers around such that their intersection sequentially go through all points on the big hull. 17 Problem 4 – Solution Time complexity: O ( n ) aħter convex hull
What is the shortest path? How do we navigate a (convex polygon shaped) robot around convex polygon obstacles? 18 Problem 5 – Minkowski sum Figure 9: Navigating a convex robot around convex obstacle
The problem would be a lot easier if we enlarge our obstacles and shrink the robot to a single point. How? Just “add” the robot and the obstacle together! Actually very easy – just merge all the points and take a convex hull! 19 Problem 5 – Solution Figure 10: Computing the Minkowski Sum of two convex polygons
Find the three points that form biggest area triangle Can we solve this with a rotating-caliper based approach? 20 Problem 6 – Biggest triangle Figure 11: Forming triangle by choosing 3 points
Unfortunately a 3 pointer walk is incorrect. Despite being a published result, a counterexample was found, and a flaw was found in the proof. There still is an alternative • Aggarwal, Alok, et al. ”Geometric applications of a matrix searching algorithm.” Proceedings of the second annual symposium on Computational geometry. ACM, 1986. • Jin, Kai. ”Maximal Area Triangles in a Convex Polygon.” 2017. 21 Problem 6 – Solution? O ( n ) method that is somewhat more complicated. INCORRECT Minimum area k -gon (generalized version) Correct solution
• Minimum/maximum distance between two convex polygons • Widest empty strip between two convex polygons • “Art gallery problem” • Intersection of two convex polygons 22 More applications of rotating calipers
Recommend
More recommend