CHAPTER 33 Computational Geometry Is the branch of
CHAPTER 33 Computational Geometry • Is the branch of computer science that studies algorithms for solving geometric problems. • Has applications in many fields, including • computer graphics • robotics, • VLSI design • computer aided design • statistics • Deals with geometric objects such as points, line segments, polygons, etc. • Some typical problems considered: • whether intersections occur in a set of lines. • finding vertices of a convex hull for points. • whether a line can be drawn separating two sets of points. • whether one point is visible from a second point, given some polygons that may block visibility. • optimal location of fire towers to view a region. • closest or most distant pair of points. • whether a point is inside or outside a polygon. Advanced Algorithms, Feodor F. Dragan, Kent State University 1
• Line segments Cross products • The convex combination of two distinct points any point such that for some real number with and • , the line segment joining and , is the set of all convex combinations of and. • Intuition problem: Show that if (x, y) is a convex combination of then is and which is the standard equation of a line with slope • Cross products • let and be points on the plane • The cross product corresponds to the signed area in the parallelogram. y x Advanced Algorithms, Feodor F. Dragan, Kent State University (0, 0) 2
• If Cross products (cont. ) is negative, then is positive, then and is counterclockwise from is clockwise from. are collinear. • To determine if we translate where is clockwise from to the origin and consider • Consider now whether two consecutive line segments and turn left or right at • Check whether . y , Prohibited for If is clockwise from (0, 0) x . is clockwise from So, counterclockwise or left turn Advanced Algorithms, Feodor F. Dragan, Kent State University So, clockwise or right turn 3
Intersection of two line segments • A two stage process is used. The first stage is called quick-rejection. • Bounding Box: The bounding box of a geometric figure is the smallest rectangle that contains the figure and whose segments are parallel to x and y axis. • The bounding box of the line segment is the box with lower lefthand point where and with upper right-hand point where • Intersection of bounding boxes: Rectangles only if the following is true: and intersect if and • this will not pass the quick rejection test (0, 0) Advanced Algorithms, Feodor F. Dragan, Kent State University 4
Intersection of two line segments (cont. ) • Second stage: Decide whether each segment meets (“straddles”) the line containing the other. • A segment straddles a line if lies on one side of the line and on the other side. (the segment straddles the line also if or lies on the line) • Observation: Two segments intersect iff they pass the quick rejection test and each segment straddles the other. • Testing straddle with cross products: • we show to check if straddles the line L determined by and If does straddle the line containing and , then the following have different signs. Boundary cases where straddles L At least one cross product is zero. Both cases pass the quick rejection test. Advanced Algorithms, Feodor F. Dragan, Kent State University 5
Determining whether any pair of segments intersect. • Given n line segments, determine whether any pair of them intersect. • Sweeping: An imaginary vertical sweep line moves across the x-axis (which acts as a time axis) from left to right. • All line segment endpoints are considered in a left-to-right order. • Whenever endpoint is found, a check for an intersection is made. • Algorithm runs in O(n logn) time, where n is the number of segments. • Algorithm does not look for all intersections, but just whether or not there is at least one intersection. • It takes time in the worst case to find all intersections (see Exercise 33. 2 -1) • Standard simplifying assumptions: • no input segment is vertical • no three input segments intersect at a single point. • Order definition: Let s 1 and s 2 be two segments s 1 • These segments are comparable at t if the vertical sweep line with x-coordinate t intersects both segments. s 2 • If s 1 and s 2 are comparable at t, then s 1 is above s 2, written , if s 1 intersects the sweep line t x at t at a higher point than s 2. Advanced Algorithms, Feodor F. Dragan, Kent State University 6
• is a total order for each value of x (reflexive, antisymmetric, transitive) • order changes as x changes (sweep line status and event point schedule). • a segment obtains an order when its left endpoint is encountered by sweep line. • a segment looses its order when sweep line reaches its right endpoint. • sweep line behavior near an intersection point a • if the intersection point is not on a sweep line, then there exist lines v and w immediately to the right and left of the intersection. • the segment that has the higher intersection at v reverses its order with other segment as sweep line passes from v to w. p b v w x • if p is the leftmost intersection point and is at intersection of segments a and b, then • a and b intersect v at adjacent points • a and b are called consecutive at v. Advanced Algorithms, Feodor F. Dragan, Kent State University 7
The Algorithm ANY-SEGMENTS-INTERSECTION(S) • To maintain a total ordering T on the sweep line as it moves, we need the following operations: • INSERT(T, s) insert segment s into T • DELETE(T, s) delete segment s from T • ABOVE(T, s) return the segment from T immediately above segment s • BELOW(T, s) return the segment from T immediately below segment s • Red-black trees can provide a “balanced” search tree which can support above operations in O(log n) time (see Ch. 13 in CLRS) (1) T emptyset (2) Sort segment endpoints from left to right. Break ties by putting point with lower y-coordinate first. (3) For each point p in the sorted list (in order) (4) if p is left endpoint of segment s then (5) INSERT(T, s) (6) if [ABOVE(T, s) exists and it intersects s] or (7) TRUE [BELOW(T, s) exists and it intersects s] then return (7) (8) if p is the right endpoint of segment then if both [ABOVE(T, s) and BELOW(T, s) exist] and [ABOVE(T, s) intersects BELOW(T, s)] then return TRUE (9) DELETE(T, s) (10) return FALSE Advanced Algorithms, Feodor F. Dragan, Kent State University Animated demo http: //www. lupinho. de/gishur/html/Sweeps. html#segment 8
Comments and Correctness • Sorting on line (2) will sort points lexicographically and require O(n logn) time. • A boundary case occurs if point p in algorithm also lies on another segment s’. In this case s and s’ should be in consecutive order at p. Correctness Theorem: A call to ANY-SEGMENTS-INTERSECTION(S) returns true iff there is an intersection among the segments of S. • If a true is returned, an intersection has been found between the two segments. • Suppose a false is returned, but an intersection actually exists. • Let p be the leftmost intersection point (break ties by taking lowest y-coordinate). • Let a and b be segments intersecting at p. • Since no intersection occurs to left of p, T gives the correct order prior to p. • that is, no reversals in order have occurred. • Since no three segments intersect at the same point, there exists a sweep line z at which a and b become consecutive. a • z is to the left of p or goes through p. • There is an endpoint q on z that is the event at which a and b become consecutive. • If p is on sweep line z, then p=q. If not, then q is left of p. Advanced Algorithms, Feodor F. Dragan, Kent State University c p q b z x 9
Correctness and Runtime • • In either case, the order of T is correct before q is processed. Only two cases are possible. 1. Either a or b is inserted into T at z and the other segment is above or below it. (In this case, intersection is detected by line (6). ) 2. Segments a and b are already in T and a segment c between them is deleted at z. (In this case, an intersection is detected by line (8). ) • In either case the intersection of a and b at p is detected, so procedure cannot return a FALSE, as supposed. a c p q b z x • Running Time: • sort on line (2) takes O(n logn) time, using merge or heap sort. • since there are 2 n events (endpoints) where sweep line stops, the loop iterates at most 2 n times. • each iteration of loop takes O(log n) time, since red-black tree operations take O(log n) time. • Total time is O(n logn). READ Ch. 33. 1 and Ch 33. 2 in CLRS. Advanced Algorithms, Feodor F. Dragan, Kent State University 10
Convex Hull Algorithms • Definitions and Properties: Given n points on the plane – – – Intersection of all convex sets containing Q Smallest convex set containing Q Intersection of all half-planes containing Q Union of all triangles formed by points of Q Smallest convex polygon containing Q – All vertices of hull are some points of Q rubber band always unique extreme point not extreme point NOTE: convex-hull(Q) is the closed solid region, not just the boundary CH(Q) Advanced Algorithms, Feodor F. Dragan, Kent State University 11
The Problem and Approaches • Problem: Given n points on the plane • Approaches: • Brute Force find CH(Q). • Gift Wrapping • Quick. Hull • Graham Scan • Incremental • Divide and Conquer • By Delaunay Triangulation & Voronoi Diagram • Brute-Force Approach q p • Determine extreme edges: for each pair of points p, q Q do if all other points lie on one side of line passing thru p and q then keep edge (p, q) • Sort edges in counterclockwise order (we want output in counterclockwise) • Running time: O(n 3) – bad but not the worst yet Advanced Algorithms, Feodor F. Dragan, Kent State University 12
Gift Wrapping or The Jarvis March Algorithm • First suggested by Chand Kapur (1970). Then by Jarvis (1973). • Worst-case time: O(n 2) • Output-sensitive time: O(nh), where h is the # of vertices of hull • Can be extended to higher dimension – was the primary algorithm for higher dimensions for quite some time Method: – Let p 0 be the lowest point (take leftmost point in case of tie) – The next convex hull vertex p 1 has the least polar angle w. r. t. the positive horizontal ray from p 0. – Measure in counterclockwise – If tie, choose the furthest such point – Vertices p 2, p 3, …, pk are picked similarly where pk is the highest point (leftmost) – The sequence p 0, p 1, p 2, p 3, …, pk is the right chain of CH(Q). – To choose the left chain of CH(Q), start with pk • choose pk+1 as the point with least angle pk w. r. t. the negative horizontal ray from pk again, measure in counterclockwise direction and if tie occurs, choose furthest such point. • continue in same fashion until you obtain pk, pk+1, …, pt= p 0 Advanced Algorithms, Feodor F. Dragan, Kent State University p 0 13
Complexity of Jarvis March • For each vertex p from CH(Q), it takes • O(1) time to compare the polar angle of and using cross-product • O(n) time to find minimum polar angle • O(n) total time • If CH(Q) has h vertices, then the running time is O(nh). • In worst case h=O(n), hence is worst case time bound. Worst case occurs when O(n) of points lie on the convex hull, e. g. , all points lie on a circle. • If points in Q are generated by random generator, then we expect h= O(logn) • In practice, Jarvis march is normally very fast. • If h=o(log n), then this algorithm is asymptotically fastest. Theorem: is a lower bound for computing the convex hull. • Assume we want to sort n numbers • create n points on plane where • construct • get from description of CH original numbers in sorted order y • Conclusion: If we can construct CH in o(n logn) time then we can sort n numbers in o(n logn) time, which is impossible. Advanced Algorithms, Feodor F. Dragan, Kent State University (0, 0) x 14
Graham Scan Algorithm • First a base point p 0 is selected. Normally this is the point with minimum ycoordinate (select leftmost in case of tie) • Next all points are sorted w. r. t. the polar angle they make with a half-ray with left endpoint p 0 and parallel to x-axis. (!!! We do not need to compute those angles!!!) p 4 remove Interior points on ray cannot be p 3 convex hull points and are removed during sort. p 6 p 5 p 2 p 1 p 7 p 0 • Remaining points are stored in counterclockwise order w. r. t. p 0 • Let p 0, p 1, p 2, p 3, …, pm be the sorted list of remaining points. • Clearly, p 0 and p 1 are in CH(Q). • Let S be a stack in which points that are potentially convex hull points will be stored. • Initially S = p 0 p 1 p 2. Remaining steps of the algorithm follow for i 3 to m do while (the angle formed by points NEXT_TO_TOP(S) , TOP(S), and pi make a non-left turn) POP(S) PUSH(S, pi) return S Advanced Algorithms, Feodor F. Dragan, Kent State University 15
Example and Runtime Computation p 5 p 7 p 4 Note straight line here. p 4 is eliminated, as non-left turn. p 3 p 6 p 2 p 1 p 8 CH(Q)={p 0, p 1, p 3, p 5, p 7 , p 8} p 0 • Requires O(n) to find p 0 • Sorting based on polar angle takes O(n logn) time • Removal of n-m points with duplicate angles takes O(n). • For loop is executed m-2 times, hence O(n). • Interior while statement is a “problem”. It may iterate as many as O(n) time. • Above observation can easily lead to an over-estimate of • Note that each pass through while statement, POP is executed. • As in analysis of MULTIPOP, there is at most one POP operation for each PUSH operation (see amortized analysis) • Since p 0, p 1, pm are not popped, at most m-3 POP operations occur. • Note, both POP and test for while take O(1) time and. Hence amortized cost for each iteration of while loop is O(1). • Overall worst-case cost of for-loop is O(n) • Worst-case running time of the algorithm is O(n logn) (= O(n logn) + O(n)). Advanced Algorithms, Feodor F. Dragan, Kent State University 16
Correctness of Graham Scan Claim 1: Each point p popped from stack S is not a vertex of CH(Q) and lies inside new CH(S). • Suppose pj is popped from S because makes a non-left turn. • Since points are ordered in increasing polar angle w. r. t p 0 , pi the points lie in counterclockwise order w. r. t. p 0 pk and contains pj pj • Then pj cannot be a vertex of CH(Q). Claim 2: The points on stack S always form the p 0 p 1 vertices of a convex polygon. • The claim holds initially for points p 0, p 1, p 2. They form a triangle. • The stack changes by points being popped or pushed. • If a point is popped, then a vertex is removed from convex polygon, leaving the resulting polygon convex. • If a point pi is pushed, the resulting points of S form a convex polygon. • by choice of p 0, p 1, pi lies above p 0 p 1 (see diagram on next slide). . • since a left turn occurs at pj, pi lies below pj pk. • since pi occurs later in the sequence than pj , the polar angle of p 0 pi. is greater than the polar angle of p 0 pj. • thus, pi lies to the left of line p 0 pj, , i. e. , pi lies in the shaded region in diagram. • so, after pi is pushed on the stack, a convex region results. Advanced Algorithms, Feodor F. Dragan, Kent State University 17
Correctness of Graham Scan (cont. ) pj Diagram pk pi p 0 p 1 Claim 3: At the conclusion of the algorithm, the points of S joined together consequently form a convex polygon that contains Q. • By claim 1, all points dropped from S are not vertices of CH(Q). • By claim 2, the points of S are points from Q that form a convex polygon • By construction and claim 1, all points of Q are either in S or lie interior to CH(S’) for some earlier S’ with CH(S’) CH(S). • Then, at conclusion the points of S joined together consequently give a convex polygon that contains all points of Q. Animated demos http: //www. cs. princeton. edu/~ah/alg_anim/version 0/Graham. html http: //www. gris. uni-tuebingen. de/gris/grdev/java/algo/solutions/lesson 12/Convex. Hull. html http: //www. piler. com/convexhull/ http: //www. geom. umn. edu/software/qhull/ Advanced Algorithms, Feodor F. Dragan, Kent State University 18
Closest Pair of Points • Given n points on the plane, find closest pair of points. – The Euclidean distance between two points and is – An obvious but naïve approach is to compute the distance between any two points and take minimum. However, running time is • A high-level description of a much better algorithm (at least for large sets) is given below. • Let Q be a set of n planar points. • If |Q|<4, then the distances between all pairs of points are computed and the closest pair is reported. • If |Q|>3, then a “Divide & Conquer & Combine” procedure is followed. • Each recursive call receives as input • a set P Q • arrays X and Y containing points P sorted by x and y coordinates, respectively Advanced Algorithms, Feodor F. Dragan, Kent State University 19
Divide and Conquer Steps Divide step • Find a vertical line L that bisects the points in P into P’ and P’’ with almost equal sizes. All points in P’ are left of L or on L, all points in P’’ are right of L or on L. • The array X is divided into arrays X’ and X’’ which contain the points of P’ and P’’, respectively, sorted by x-coordinates. • Likewise the array Y is split into arrays Y’ and Y’’ which contain the points of P’ and P’’, respectively, sorted by y-coordinates. • At the beginning, Q has to be presorted by x-coordinates to obtain X and by ycoordinates to obtain Y. This we do before the first recursive call. • Now, on each recursive call, the set P can easily be split in linear time, giving sets P’, P’’, X’, and X’’. • Both Y’ and Y’’ can be formed by Y in linear time by walking through Y and putting each element in Y’ (or Y’’) if it is in P’ (respectively, P’’). Conquer Step • Make two recursive calls to find closest pair of points in P’ and P’’, respectively. • First call gets input (P’, X’, Y’) • Second call gets input (P’’, X’’, Y’’) • Set closest pair returned for P’ and P’’ have distance d’ and d’’, respectively. • Set d = min{d’, d’’} Advanced Algorithms, Feodor F. Dragan, Kent State University 20
Combine Step and Runtime Combine • Observe that if there is a pair in P closer than d, then they must lie in a box about L, as follows. • There can be at most 8 points in each strip. • Find the array Y consisting of the points of Y within d of L, sorted by their y-coordinate. d d L L Possible coincident points, one in P’ and one in P’’. • Next, for each point p from Y, check to see if d(p, q)<d for each of the 7 points q that follow p in Y. • Find the closest-pair distance d, computed in preceding step over all points p in Y. • If d<d, then the closest pair with distance d is returned. Otherwise, the closest pair formed by the two recursive calls is returned. Runtime • The initial preprocessing step costs O(n logn) • The recurrence relation for the recursive portion is T(n), where • Its solution is T(n)= O(n logn). This gives also the total running time. Advanced Algorithms, Feodor F. Dragan, Kent State University READ Ch. 33. 3 and 33. 4 in CLRS. 21
- Slides: 21