1 / 65

Computational Geometry

Computational Geometry. Instructor: yedeshi yedeshi@gmail.com. Computational Geometry. Algorithms for geometric problems Applications: CAD, GIS, computervision,……. E.g., the closest pair problem: Given: a set of points P={p 1 … p n } in the plane, such that p i =(x i ,y i )

nchristy
Download Presentation

Computational Geometry

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computational Geometry Instructor: yedeshi yedeshi@gmail.com

  2. Computational Geometry • Algorithms for geometric problems • Applications: CAD, GIS, computervision,……. • E.g., the closest pairproblem: • Given: a set of points P={p1… pn} in the plane, such that pi=(xi,yi) • Goal: find a pair pi≠pjthat minimizes ||pi –pj||, • where ||p-q||= [(px-qx)2+(py-qy)2]1/2

  3. Line-segment • A convex combination of two distinct points p1 = (x1, y1) and p2 = (x2, y2) is any point p3 = (x3, y3) such that for some α in the range 0 ≤ α ≤ 1, we have x3 = αx1 + (1 - α)x2 and y3 = αy1 + (1 - α)y2. Also, p3 = αp1 + (1 - α)p2. • Line segment:Given two distinct points p1 and p2, the line segment is the set of convex combinations of p1 and p2. We call p1 and p2 the endpoints of segment • Sometimes the ordering of p1 and p2 matters, and we speak of the directed segment

  4. Cross products • Computing cross products is at the heart of our line-segment methods • Consider vectors p1 and p2 : • The cross productp1 × p2 can be interpreted as the signed area of the parallelogram formed by the points (0, 0), p1, p2, and p1 + p2 = (x1 + x2, y1 + y2).

  5. Cross product • An equivalent, but more useful, definition gives the cross product as the determinant of a matrix

  6. Cross product • If p1 × p2 is positive, then p1 is clockwise from p2 with respect to the origin (0, 0); If it is negative, then p1 is counterclockwise from p2 • Boundary condition: cross product is 0; in this case, the vectors are collinear, pointing in either the same or opposite directions. p1 × p2 is positive clockwise and counterclockwise regions relative to a vector p

  7. clockwise • To determine whether a directed segmentis clockwise from a directed segment ? • Compute • (p1 - p0) × (p2 - p0) = (x1 - x0)(y2 - y0) - (x2 - x0)(y1 - y0). (a) If counterclockwise, the points make a left turn (b) If clockwise, they make a right turn

  8. Two line segments intersect • Determine whether two line segments intersect, we check whether each segment straddles the line containing the other. • A segment straddles a line if point p1 lies on one side of the line and point p2 lies on the other side. • Two line segments intersect if and only if either (or both) of the following conditions holds: • Each segment straddles the line containing the other. • An endpoint of one segment lies on the other segment. (This condition comes from the boundary case.)

  9. Algorithm SEGMENTS-INTERSECT(p1, p2, p3, p4) 1 d1 ← DIRECTION(p3, p4, p1) 2 d2 ← DIRECTION(p3, p4, p2) 3 d3 ← DIRECTION(p1, p2, p3) 4 d4 ← DIRECTION(p1, p2, p4) 5 if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) and ((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)) 6 then return TRUE 7 elseifd1 = 0 and ON-SEGMENT(p3, p4, p1) 8 then return TRUE 9 elseifd2 = 0 and ON-SEGMENT(p3, p4, p2) 10 then return TRUE 11 elseifd3 = 0 and ON-SEGMENT(p1, p2, p3) 12 then return TRUE 13 elseifd4 = 0 and ON-SEGMENT(p1, p2, p4) 14 then return TRUE 15 else return FALSE

  10. Algorithm con. • DIRECTION: computes relative orientations using the cross-product method • ON-SEGMENT: which determines whether a point known to be collinear with a segment lies on that segment.

  11. Algorithm con. • DIRECTION(pi, pj, pk) • 1 return (pk - pi) × (pj - pi) • ON-SEGMENT(pi, pj, pk) • 1 if min(xi, xj) ≤ xk ≤ max(xi, xj) and min(yi, yj) ≤ yk ≤ max(yi, yj) • 2 then return TRUE • 3 else return FALSE

  12. Segment intersection • Given: a set of ndistinct segments s1…sn, represented by coordinates of endpoints • Detection: detect if there is any pair si≠ sjthat intersects • Reporting: report all pairs of intersecting segments

  13. Segment intersection • Easy to solve in O(n2)time. • Is it possible to get a better algorithm for the reporting problem? • NO ! (in the worst-case) How to prove it? • However: • We will see we can do better for the detection problem • Moreover, the number of intersections P is usually small. • Then, we would like an output sensitive algorithm, whose running time is low if P is small.

  14. Θ(n2) intersections

  15. Result • We will show: • O(n log n) time for detection • O( (n +P) log n) time for reporting • We will use • …… (no, not divide and conquer) • … Binary Search Trees • Specifically: Line sweep approach

  16. Determining segments intersects • Determining whether any two line segments in a set of segments intersect. • Sweeping: is common to many computational-geometry algorithms. An imaginary vertical sweep line passes through the given set of geometric objects, usually from left to right. • Ordering segments: • Two segments s1 and s2 , • comparable at x if the vertical sweep line with x-coordinate x intersects both of them

  17. Order segments • s1 is aboves2 at x, written s1 >xs2, if s1 and s2 are comparable at x and the intersection of s1 with the sweep line at x is higher than the intersection of s2 with the same sweep line. • For example, we have the relationships a >rc, a >tb, b >tc, a >tc, and b >uc. Segment d is not comparable with any other segment.When segments e and fintersect, their orders are reversed: we have e >v f but f >w e.

  18. Moving the sweep line • Sweeping algorithms typically manage two sets of data: • The sweep-line status gives the relationships among the objects intersected by the sweep line. • The event-point schedule is a sequence of x-coordinates, ordered from left to right, that defines the halting positions of the sweep line. We call each such halting position an event point. Changes to the sweep-line status occur only at event points. • The sweep-line status is a total order T, for which we require 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 immediately above segment s in T. • BELOW(T, s): return the segment immediately below segment s in T. • If there are n segments in the input, we can perform each of the above operations in O(lg n) time using red-black trees.

  19. Segment-intersection pseudocode ANY-SEGMENTS-INTERSECT(S) 1 T ← Ø 2 sort the endpoints of the segments in S from left to right, breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y-coordinates first 3 for each point p in the sorted list of endpoints 4 do ifp is the left endpoint of a segment s 5 then INSERT(T, s) 6 if (ABOVE(T, s) exists and intersects s) or (BELOW(T, s) exists and intersects s) 7 then returnTRUE 8 ifp is the right endpoint of a segment s 9then ifboth ABOVE(T, s) and BELOW(T, s) exist and ABOVE(T, s) intersects BELOW(T, s) 10 then returnTRUE 11 DELETE(T, s) 12 return FALSE

  20. Example Each dashed line is the sweep line at an event point, and the ordering of segment names below each sweep line is the total order T at the end of the for loop in which the corresponding event point is processed. The intersection of segments d and b is found when segment c is deleted.

  21. Correctness • We will prove that the call ANY-SEGMENTS-INTERSECT(S) returns TRUE if and only if there is an intersection among the segments in S. • if it returns TRUE, there is an intersection. • if there is an intersection, then ANY-SEGMENTS-INTERSECT returns TRUE • Let us suppose that there is at least one intersection. • Let p be the leftmost intersection point. And let a and b be the segments that intersect at p. • There exists a sweep line z at which a and b become consecutive in the total order. z is to the left of p or goes through p. • There exists a segment endpoint q on sweep line z that is the event point at which a and b become consecutive in the total order. • If p is on sweep line z, then q = p. If p is not on sweep line z, then q is to the left of p.

  22. Correctness • If q is processed by ANY-SEGMENTS-INTERSECT, there are only two possibilities for the action taken: • Either a or b is inserted into T, and the other segment is above or below it in the total order. Lines 4-7 detect this case. • Segments a and b are already in T, and a segment between them in the total order is deleted, making a and b become consecutive. Lines 8-11 detect this case. • In either case, the intersection p is found and ANY-SEGMENTS-INTERSECT returns TRUE.

  23. Running time • If there are n segments in set S, then ANY-SEGMENTS-INTERSECT runs in time O(n lg n). • Line 1 takes O(1) time. • Line 2 takes O(n lg n) time, using merge sort or heapsort. Since there are 2n event points, the for loop of lines 3-11 iterates at most 2n times. • Each iteration takes O(lg n) time, since each red-black-tree operation takes O(lg n) time and, using the method of line intersection, each intersection test takes O(1) time. • The total time is thus O(n lg n).

  24. The closet pair problem • the closest pairproblem: • Given: a set of points P={p1… pn} in the plane, such that pi=(xi,yi) • Goal: find a pair pi≠pjthat minimizes ||pi –pj||, • where ||p-q||= [(px-qx)2+(py-qy)2]1/2

  25. Closest Pair • Find a closest pair among p1…pn • Easy to do in O(n2) time • For all pi≠pj, compute || pi –pj|| and choose the minimum • We will aim for O(n log n)time

  26. Divide and conquer • Divide: • Compute the median of x-coordinates • Split the points into PL and PR, each of size n/2 • Conquer: compute the closest pairs for PL and PR • Combine the results (the hard part) PR PL

  27. Combine 2d • Let d=min(d1,d2) • Observe: • Need to check only pairs which cross the dividing line • Only interested in pairs within distance < d • Suffices to look at points in the 2d-width strip around the median line d2 d1

  28. Scanning the strip • Sort all points in the strip by their • y-coordinates, forming q1…qk, k ≤ n. • Let yi be the y-coordinate of qi • dmin= d • Fori=1 tok • j=i-1 Whileyi- yj< d If||qi–qj||<d then dmin=||qi–qj|| j:=j-1 • Report dmin(and the corresponding pair)

  29. Analysis • Correctness: easy • Running time is more involved • Can we have many qj’s that are within distance d from qi ? • No • Proof by packing argument d

  30. Analysis, ctd. d × d • Theorem:there are at most 7 qj’s such that yi-yj ≤ d. • Pf: Each such qjmust lie either in the left or in the right d × d square • Note that within each square, all points have distance ≥ d from others • We can pack at most 4 such points into one square, so we have 8 points total (incl. qi) qi

  31. Running time • Divide:O(n) • Combine:O(n log n) because we sort by y • However, we can: • Sort all points by y at the beginning • Divide preserves the y-order of points Then combine takes only O(n) • We get T(n)=2T(n/2)+O(n), soT(n)=O(n log n)

  32. Finding the convex hull • The convex hull of a set Q of points is the smallest convex polygon P for which each point in Q is either on the boundary of P or in its interior. • Ex. CH(Q): p0 , p1 , p3, p10 , p12.

  33. Graham's scan • Graham's scan solves the convex-hull problem by maintaining a stack S of candidate points. • Each point of the input set Q is pushed once onto the stack, and the points that are not vertices of CH(Q) are eventually popped from the stack. • When the algorithm terminates, stack S contains exactly the vertices of CH(Q), in counterclockwise order of their appearance on the boundary.

  34. GRAHAM-SCAN • The procedure GRAHAM-SCAN takes as input a set Q of points, where |Q| ≥ 3. • It calls the functions TOP(S), which returns the point on top of stack S without changing S, and NEXT-TO-TOP(S), which returns the point one entry below the top of stack S without changing S. • As we shall prove in a moment, the stack S returned by GRAHAM-SCAN contains, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order.

  35. Pseudocode GRAHAM-SCAN(Q) 1 let p0 be the point in Q with the minimum y-coordinate, or the leftmost such point in case of a tie 2 let 〈p1, p2, ..., pm〉 be the remaining points in Q, sorted by polar angle in counterclockwise order around p0 (if more than one point has the same angle, remove all but the one that is farthest from p0) 3 PUSH(p0, S) 4 PUSH(p1, S) 5 PUSH(p2, S) 6 fori ← 3 tom 7 do while the angle formed by points NEXT-TO-TOP(S), TOP(S), and pi makes a nonleft turn 8 do POP(S) 9 PUSH(pi, S) 10 returnS

  36. Illustration: sorts the remaining points of Q by polar angle relative to p0, using the method comparing cross products

  37. Theorem • Theorem. If GRAHAM-SCAN is run on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. • Pf. After line 2, we have the sequence of points 〈p1, p2, ..., pm〉. Let us define, for i = 2, 3, ..., m, the subset of points Qi = {p0, p1, ..., pi}.The points in Q - Qm are those that were removed because they had the same polar angle relative to p0 as some point in Qm; these points are not in CH(Q), and so CH(Qm) = CH(Q). • Note that just as p0, p1, and pm are vertices of CH(Q), the points p0, p1, and pi are all vertices of CH(Qi)

  38. Prof. • At the start of each iteration of the for loop of lines 6-9, stack S consists of, from bottom to top, exactly the vertices of CH(Qi-1) in counterclockwise order. • Initialization: The invariant holds the first time we execute line 6, since at that time, stack S consists of exactly the vertices of Q2 = Qi-1, and this set of three vertices forms its own convex hull. Moreover, they appear in counterclockwise order from bottom to top. • Maintenance: Entering an iteration of theforloop, the top point on stack S is pi-1. • Let pj be the top point on S after the while loop of lines 7-8 is executed but before line 9 pushes pi, and let pk be the point just below pj on S.

  39. Prof. • Focus on this moment just before pi is pushed. • Because pi's polar angle relative to p0 is greater than pj's polar angle, and because the angle makes a left turn (otherwise we would have popped pj )

  40. Pf. • We now show that CH(Qj ∪ {pi}) is the same set of points as CH(Qi). • Consider any point ptthat was popped during iteration i of the for loop, and let pr be the point just below pt on stack S at the time pt was popped (pr might be pj ). • The angle ∠pr pt pi makes a nonleft turn, and the polar angle of pt relative to p0 is greater than the polar angle of pr • pt must be either in the interior of the triangle formed by p0, pr, and pi or on a side of this triangle (but it is not a vertex of the triangle). • Clearly, since pt is within a triangle formed by three other points of Qi, it cannot be a vertex of CH(Qi). Since pt is not a vertex of CH(Qi),

  41. Pf. • CH(Qi-{pt}) = CH(Qi) , • Let Pi be the set of points that were popped during iteration i of the for loop. CH(Qi - Pi) = CH(Qi). • But Qi - Pi = Qj ∪ {pi}, and so we conclude that CH(Qj ∪ {pi}) = CH(Qi - Pi) = CH(Qi). • We have shown that once we push pi , stack S contains exactly the vertices of CH(Qi) in counterclockwise order from bottom to top. Incrementing i will then cause the loop invariant to hold for the next iteration. • Termination: When the loop terminates, we have i = m + 1, and so the loop invariant implies that stack S consists of exactly the vertices of CH(Qm), which is CH(Q), in counterclockwise order from bottom to top. This completes the proof.

  42. Running time • Line 1 takes Θ(n) time. • Line 2 takes O(n lg n) time, using merge sort or heapsort to sort the polar angles and the cross-product method of to compare angles. (Removing all but the farthest point with the same polar angle can be done in a total of O(n) time.) • Lines 3-5 take O(1) time. Because m ≤ n - 1, the for loop of lines 6-9 is executed at most n - 3 times. Since PUSH takes O(1) time, each iteration takes O(1) time exclusive of the time spent in the while loop of lines 7-8, and thus overall the for loop takes O(n) time exclusive of the nested while loop. • We use aggregate analysis to show that the while loop takes O(n) time overall. • For i = 0, 1, ..., m, each point piis pushed onto stack S exactly once. We observe that there is at most one POP operation for each PUSH operation. At least three points-p0, p1, and pm-are never popped from the stack, so that in fact at most m - 2 POP operations are performed in total. Each iteration of the while loop performs one POP, and so there are at most m - 2 iterations of the while loop altogether. Since the test in line 7 takes O(1) time, each call of POP takes O(1) time, and since m ≤ n - 1, the total time taken by the while loop is O(n). • Thus, the running time of GRAHAM-SCAN is O(n lg n).

  43. Jarvis's march • Jarvis's march computes the convex hull of a set Q of points by a technique known as package wrapping (or gift wrapping). • The algorithm runs in time O(nh), where h is the number of vertices of CH(Q). When h is o(lg n), Jarvis's march is asymptotically faster than Graham's scan. • Jarvis's march builds a sequence H = 〈p0, p1, ..., ph-1〉 of the vertices of CH(Q). • We start with p0, the next convex hull vertex p1 has the smallest polar angle with respect to p0. (In case of ties, we choose the point farthest from p0.) Similarly, p2 has the smallest polar angle with respect to p1, and so on. • When we reach the highest vertex, say pk(breaking ties by choosing the farthest such vertex), we have constructed the right chain of CH(Q). • To construct the left chain, we start at pk and choose pk+1 as the point with the smallest polar angle with respect to pk, but from the negative x-axis. We continue on, forming the left chain by taking polar angles from the negative x-axis, until we come back to our original vertex p0.

  44. Illustration

  45. Running time • If implemented properly, Jarvis's march has a running time of O(nh). • For each of the h vertices of CH(Q), we find the vertex with the minimum polar angle. • Each comparison between polar angles takes O(1) time • We can compute the minimum of n values in O(n) time if each comparison takes O(1) time. • Thus, Jarvis's march takes O(nh) time.

  46. Divide-and-conquer • Divide: choosing a pivot point p. Partitions the input points into two sets L and R. • L: containing the points to the left of p, including p itself. • R: the points to the right of p. (by comparing x-coordinates.) • Conquer: Recursively solve it. • Combine: the hardest one.

  47. Merge • Connecting the two hulls with a line segment between the rightmost point of the hull of L with the leftmost point of the hull of R. Call these points p and q. Actually, let's add two copies ofthe segment pq and call them bridges. • Merge: • scanning around the left hull in a clockwise direction (from bridge to points in L) and around the right hull in an anti-clockwise direction (from points in R to bridge). • If above turns happen, update the bridge accordingly.

  48. Illustration of combining p p p q q II I q III

  49. Illustration of combining p p q q V p IV q VI

  50. Up to date • Graham: O(n log n) • Jarvis: O(n h) • Yao: lower bound O(n log n) • Kirkpatrick and Seidel: O(n lg h) 86 • Timothy Chan, O(n lg h) 93

More Related