Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Computational Geometry Tommy Färnqvist Seminar 13 Computational Geometry Outline TDDD95: APS Primitive Operations Seminar in Algorithmic Problem Solving May 3, 2016 Polygon Area Convex Hull Closest Pair Final Note Tommy Färnqvist Department of Computer and Information Science Linköping University 13.1 Outline Computational Geometry Tommy Färnqvist 1 Primitive Operations Outline Primitive Operations Polygon Area 2 Polygon Area Convex Hull Closest Pair Final Note 3 Convex Hull 4 Closest Pair 13.2 Geometric Primitives Computational Geometry Tommy Färnqvist • Point: two numbers (x, y ). • Line: two numbers a and b. [ax + by = 1] Outline • Line segment: two points. Primitive Operations • Polygon: sequence of points. Polygon Area Convex Hull Closest Pair Primitive operations Final Note • Is a polygon simple? • Is a point inside a polygon? • Do two line segments intersect? • What is the Euclidean distance between two points? • Given tree points p1 , p2 , p3 , is p1 → p2 → p3 a counterclockwise turn? 13.3 Geometric Intuition Warning: intution may be misleading. Computational Geometry Tommy Färnqvist • Humans have spatial intuition in 2D and 3D. • Computers do not. • Neither has good intuition in higher dimensions! Q. Is a given polygon simple? (No crossings.) Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.4 Polygon inside, outside Computational Geometry Tommy Färnqvist Theorem (Jordan curve theorem, Jordan 1886, Veblen 1905) Any continuous simple closed curve cuts the plane in exactly two pieces: the inside and the outside. Q. Is a point inside a simple polygon? Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.5 Polygon inside, outside: crossing number Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.6 Implementing ccw Computational Geometry Tommy Färnqvist CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn? • Analog of compares in sorting • Idea: compare slopes Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note Lesson. Geometric primitives are tricky to implement. • Dealing with degenerate cases. • Coping with floating-point precision. 13.7 Implementing ccw CCW. Given three points a, b, and c, is a → b → c a counterclockwise turn? Computational Geometry Tommy Färnqvist • Determinant gives twice signed area of triangle. Outline Primitive Operations Polygon Area Convex Hull • If area > 0 then a → b → c is counterclockwise. • If area < 0 then a → b → c is clockwise. Closest Pair Final Note • If area = 0 then a → b → c are collinear. 13.8 Immutable Point Data Type Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.9 Sample ccw client: line intersection Computational Geometry Tommy Färnqvist Intersect. Given two line segments, do they intersect? • Idea 1: find intersection point using algebra and check. • Idea 2: check if the endpoints of one line segment are on different “sides” of the other line segment (4 calls to ccw). Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.10 Area of Polygons Area. The area of any simple polygon is given by the following formula A= n X (xk +1 + xk )(yk +1 − yk ) 2 k =0 , where n is the number of vertices, (xk , yk ) is the k th point when labelled in a counterclockwise manner, and (xn+1 , yn+1 ) = (x0 , y0 ). Example A= n X (xk +1 + xk )(yk +1 − yk ) 2 k =0 (1.5 + 0)(0 − 0) (2.5 + 1.5)(−1 − 0) (3.5 + 2.5)(0 − (−1)) (5 + 3.5)(0 − 0) + + + 2 2 2 2 (3.5 + 5)(2 − 2) (2.5 + 3.5)(3 − 2) (5 + 5)(2 − 0) + + + 2 2 2 (1.5 + 2.5)(2 − 3) (0 + 1.5)(2 − 2) (0 + 0)(0 − 2) + + + 2 2 2 = 0 + (−2) + 3 + 0 + 10 + 0 + 3 + (−2) + 0 + 0 = = 12 Computational Geometry Area of Polygons Area. The area of any simple polygon with vertices at integer coordinates is given by Pick’s theorem: R A = I + − 1, 2 where R is the number of integer points on the boundary of the polygon, and I is the number of integer points in the interior of the polygon. Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Example Closest Pair Final Note Here, R = 12, and I = 40, which means the area of the polygon is 40 + 6 − 1 = 45. 13.12 Convex Hull Computational Geometry Tommy Färnqvist A set of points is convex if, for any two points p and q in the set, the line segment pq is completely in the set. Convex hull. Smallest convex set containing all the points. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note • Shortest (perimeter) fence surrounding the points. • Smallest (area) convex polygon enclosing the points. 13.13 Mechanical Solution Computational Geometry Tommy Färnqvist Mechanical convex hull algorithm. Hammer nails perpendicular to the plane; stretch elastic rubber band around points. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.14 Brute-force Algorithm Observation 1. Edges of convex hull of P connects pairs of points in P. Computational Geometry Tommy Färnqvist − → Observation 2. p-q is on convex hull of all other points are counterclockwise of pq. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note O(N 3 ) algorithm. For all pairs of points p and q: • Compute Point.ccw(p, q, x) for all other points x. • p → q is on hull if all valules are positive. Degeneracies. Three (or more) points on a line. 13.15 Package Wrap (Jarvis March) • Start with point with smallest y -coordinate. • Rotate sweep line aroung current point in the ccw direction. • First point hit is on the hull. • Repeat. Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.16 Package Wrap (Jarvis March) Computational Geometry Tommy Färnqvist • Compute angle between current point and all remaining points. • Pick smallest angle larger than current angle. • Θ(N) per iteration. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.17 How Many Points on the Hull? Computational Geometry Tommy Färnqvist Parameters. • N = number of points. • h = number of points on the hull. Outline Primitive Operations Polygon Area Convex Hull Package wrap running time. Θ(Nh). Closest Pair Final Note How many points on the hull? • Worst case: h = N. • Average case: difficult problems in stochastic geometry. • uniformly at random in a disc: h = N 1/3 • uniformly at random in a convex polygon with O(1) edges: h = log N 13.18 Graham Scan • Choose point p with smallest y -coordinate. Computational Geometry Tommy Färnqvist • Sort points by polar angle with p to get (simple) polygon. • Consider points in order, and discard those that would create clockwise turn. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.19 Graham Scan: Implementation Computational Geometry Tommy Färnqvist • Input: p[1], p[2], ..., p[N] are distinct points (not all collinear). • Output: M and rearrangement so that p[1], p[2], ..., p[M] is convex hull. Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note Running time. N log N for sort and linear for rest. (Why?) 13.20 Quick Elemination Computational Geometry Tommy Färnqvist Quick elimination. • Choose a quadrilateral Q or rectangle R with 4 points as corners. • Any point inside cannot be on hull. • 4 ccw tests for quadrilateral • 4 compares for rectangle Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note Three-phase algorithm. • Pass through all points to compute R. • Eliminate points inside R. • Find convex hull of remaining points. In practice. Eliminates almost all points in linear time. 13.21 Closest Pair Computational Geometry Tommy Färnqvist Closest pair problem. Given N points in the plane, find a pair of points with the smallest Euclidean distance between them. Outline Primitive Operations Polygon Area Convex Hull Brute force. Check all pairs with N 2 distance calculations. Closest Pair Final Note 1d version. Easy N log N algorithm if points are on a line. Non-degeneracy assumption. No two points have the same x-coordinate. 13.22 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.23 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.24 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.25 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.26 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.27 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.28 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.29 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.30 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.31 Divide-and-conquer Algorithm Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Convex Hull Closest Pair Final Note 13.32 Next time Computational Geometry Tommy Färnqvist Outline Primitive Operations Polygon Area Combinatorics/Probability theory. . . Convex Hull Closest Pair Final Note 13.33