CS 372: Computational Geometry Lecture 3 Line Segment Intersection Antoine Vigneron King Abdullah University of Science and Technology September 9, 2012 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 1 / 32
1 Introduction 2 Preliminary 3 Intersection Detection 4 Intersection reporting Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 2 / 32
Problems Input: A set S = {s 1, s 2,... s n } of n line segments in R 2 given by the coordinates of their endpoints. s 4 s 1 s 5 s 3 s 2 s 6 Intersection detection: Is there a pair (s i, s j ) S 2 such that i j and s i s j? Intersection reporting: Find all pairs (s i, s j ) S 2 such that i j and s i s j. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 3 / 32
Motivation Motion planning: collision detection Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 4 / 32
Motivation Geographic Information Systems: Map overlay More generally: spatial join in databases ROAD MAP Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 5 / 32
Motivation Geographic Information Systems: Map overlay More generally: spatial join in databases ROAD MAP + RIVER MAP Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 6 / 32
Motivation Computer Aided Design: boolean operations P Q P \ Q Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 7 / 32
Intersection of Two Line Segments Finding the intersection of two line segments: Find the intersection of their two support lines Linear system, two variables, two equations. Check whether this intersection point is between the two endpoints of each line segment. If not, the intersection is empty. Degenerate case: same support line. Intersection may be a line segment. O(1) time. Checking whether two line segments intersect without computing the intersection point: 4 CCW ( ) tests. (How?) Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 8 / 32
First Approach Brute force algorithm: Check all pairs of segments for intersection. Running time: ( n 2 ) Θ(1) = Θ(n 2 ) Can we do better? Intersection detection: Yes, see later. Intersection reporting: If all pairs intersect there are Ω(n 2 ) intersections, then our time bound is optimal as a function of n. So we will look for an output-sensitive algorithm. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 9 / 32
Plane Sweep Algorithms Intuition: A vertical line sweeps the plane from left to right and draws the picture. Example: last week s convex hull algorithm. Current event point I know the upper hull Not handled yet Sweep line Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 10 / 32
Plane Sweep Algorithms The sweep line moves from left to right and stops at event points. Convex hull: The event points are just the input points. Sometimes they are not known from the start (see intersection reporting). We maintain invariants. Convex hull: I know the upper hull of the points to the left of the sweep line. At each event, restore the invariants. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 11 / 32
General Position Assumptions No three endpoints are collinear. Let E be the set of the endpoints. Let I be the set of intersection points. No two points in E I have same x-coordinate. No three segments intersect at the same point. Degenerate Cases s s 3 collinear endpoints same x-coordinate multiple intersection Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 12 / 32
Intersection Detection Plane sweep algorithm Let l t be the vertical line with equation x = t. S t is the sequence of segments that intersect l t, ordered by the y-coordinates of their intersections with l t. S t = (s 4, s 5, s 3 ) s 1 s 3 s 7 s 2 s 5 s 6 s 4 l t Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 13 / 32
Intersection Detection Idea: Maintain S t while l t moves from left to right until an intersection is found. Invariants: There is no intersection to the left of lt. We know St. Let t 1 < t 2 < < t 2n be the x-coordinates of the endpoints. l t stops at each time t = t i. Let f be the last index the sweep-line l ti reaches. In other words, let f be the largest index such that there is no intersection point with abscissa smaller than t f. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 14 / 32
Example No intersection l tf = l t7 l tf +1 Leftmost intersection Knowing S ti slides.) for some i < f, we can easily find S ti+1. (See next two Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 15 / 32
First Case: Left Endpoint If t i+1 corresponds to the left endpoint of s, insert s into S ti obtain S ti+1. in order to s 4 s 1 St3 = (s 2, s 3, s 1 ) s 2 s 5 s 3 S t4 = (s 2, s 3, s 4, s 1 ) l t3 l t4 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 16 / 32
Second Case: Right Endpoint Delete the corresponding segment in order to obtain S ti+1. s 4 s 1 S t4 = (s 2, s 3, s 4, s 1 ) s 2 s 5 s 3 S t5 = (s 2, s 3, s 4 ) l t4 l t5 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 17 / 32
Data Structure We maintain S t in a balanced binary search tree. For i < f, we obtain S ti+1 from S ti by performing an insertion or a deletion. Each takes O(log n) time. We did not check intersections. How to do it? Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 18 / 32
A Geometric Observation Let q = s s be the leftmost intersection point. s q s l tf = l t7 l tf +1 s and s are adjacent in S tf (proof next slide). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 19 / 32
Proof by Contradiction Assume that S tf = (... s... s... s... ). The right endpoint of s cannot be to the left of q, by definition of t f. If q is below s, then s intersect s to the left of q, which contradicts the fact that q is the leftmost intersection. Similarly, q cannot be above s. We reached a contradiction. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 20 / 32
Checking Intersection We store S t in a balanced binary search tree T, the order is the vertical order along l t. Given a segment in T, we can find in O(log n) time the next and previous segment in vertical order. When deleting a segment in T, two segments s and s become adjacent. We can find them in O(log n) time and check if they intersect. When inserting a segment s i in T, it becomes adjacent to (at most) two segments s and s. Check if s i s and if s i s. In any case, if we find an intersection, we are done. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 21 / 32
Pseudocode Line segment intersection dectection Algorithm DetectIntersection(S) Input: A set S of line segments in R 2 Output: TRUE iff there exist two intersecting segments in S 1. (e 1, e 2... e 2n ) Endpoints, ordered by increasing x-coordinate 2. T empty balanced BST 3. for i = 1 to 2n 4. if e i is left endpoint of some s S 5. then insert s into T 6. if s intersects next(s) or prev(s) 7. then return TRUE 8. if e i is right endpoint of some s S 9. if next(s) intersects prev(s) 10. then return TRUE 11. else delete s from T 12. return FALSE Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 22 / 32
Analysis Line 1: Θ(n log n) time by mergesort Line 2: O(1) time Line 5 and 6: O(log n) time Loop 3 11: O(n log n) time So our algorithm runs in Θ(n log n) time We will see later that it is optimal in some sense. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 23 / 32
Intersection Reporting Brute force: Θ(n 2 ) time, which is optimal in the worst case, when there are k = Ω(n 2 ) intersections. If there is 1 intersection, the detection algorithm does it in O(n log n) time which is better. We will be able to interpolate between these two algorithms, and find an output-sensitive algorithm. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 24 / 32
Output Sensitive Algorithm Let k = #intersecting pairs. Here k = # intersection points, since we assume general position. k is the output size. Sweep line algorithm reports intersections in O((n + k) log n) time (see later). This is an output sensitive algorithm. Ω(n + k) is a lower bound nearly optimal (within an O(log n) factor). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 25 / 32
Algorithm Similar to intersection detection. Two types of event points: endpoints and intersection points. We do not know intersection points in advance. We cannot sort them all in advance. We will use an event queue Q. Q contains event points ordered by increasing x-coordinate. Implementation: a min-heap. We can insert an event point in O(log n) time. We can dequeue the event point with smallest x-coordinate in O(log n) time. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 26 / 32
Algorithm Initially, Q contains all the endpoints. The sweep line moves from left to right. It stops at each event point of Q. When it does, we dequeue the corresponding event point. Each time we find an intersection, we insert it into Q. When we reach an intersection point, we swap the corresponding segments in T, and check for intersection the newly adjacent segments. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 27 / 32
Intersection Event s 1 s 2 s 4 s 3 S t = (s 5, s 2, s 3, s 4 ) s 5 S t = (s 5, s 3, s 2, s 4 ) l t l t Check s 4 s 2 and s 3 s 5 At time t, the event queue Q contains all the endpoints with abscissa larger than t and the intersection point s 2 s 3. At time t, we insert s 3 s 5 into Q. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 28 / 32
Analysis Each event is inserted or deleted in O(log(n + k)) time. Problem: An intersection point may be inserted several times into Q. s 1 s 2 is inserted three times. - when we reach the left endpoint of s 2 s 2 - when we reach the right endpoint of s 3 - when we reach the right endpoint of s 4 s 3 s 4 s 1 When we reach an event point that is present several times in Q, we just extract it repeatedly until a new event is found. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 29 / 32
Analysis Some intersection points are inserted several times into Q. How many times does it happen? At most twice at each endpoint or intersection point. At most 4n + 2k times in total. It follows that we insert O(n + k) events into Q. So the algorithm runs in O((n + k) log(n + k)) time. log(n + k) < log(n + n 2 ) = O(log n) The running time is O((n + k) log n). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 30 / 32
Space Complexity In the worst case, Q contains O(n + k) points. This algorithm requires O(n + k) space. Can we do better? Yes: At time t, only keep intersections between segments that are adjacent in S t. Then the algorithm requires only O(n) space. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 31 / 32
Conclusion New algorithmic paradigm: Plane Sweep. Idea: a sweep line moves from left to right and draws the picture. It stops at a finite set of event points, where the data structure is updated. The event points are stored in a priority queue. It can be used for other problems. For instance: Map overlay: compute a full description of the map. Fixed-radius near neighbor searching in 2D. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 32 / 32