Two Segments Intersect?

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Chapter 13. Red-Black Trees
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Introduction to Algorithms
CS16: Introduction to Data Structures & Algorithms
Computational Geometry The systematic study of algorithms and data structures for geometric objects, with a focus on exact algorithms that are asymptotically.
Great Theoretical Ideas in Computer Science for Some.
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Introduction to Algorithms Red-Black Trees
Geometry Introduction
Polygon Triangulation
Computational Geometry
Divide and Conquer. Subject Series-Parallel Digraphs Planarity testing.
Line Segment Intersection. Balanced binary search tree Internal nodes: Rightmost leaf in its left subtree Status Structure T.
Map Overlay Algorithm. Birch forest Wolves Map 1: Vegetation Map 2: Animals.
Chapter 4: Trees Part II - AVL Tree
Polygon overlay in double precision arithmetic One example of why robust geometric code is hard to write Jack Snoeyink & Andrea Mantler Computer Science,
Dynamic Planar Convex Hull Operations in Near- Logarithmic Amortized Time TIMOTHY M. CHAN.
2/14/13CMPS 3120 Computational Geometry1 CMPS 3120: Computational Geometry Spring 2013 Planar Subdivisions and Point Location Carola Wenk Based on: Computational.
Brute-Force Triangulation
2/3/15CMPS 3130/6130 Computational Geometry1 CMPS 3130/6130 Computational Geometry Spring 2015 Triangulations and Guarding Art Galleries II Carola Wenk.
Convex Hull obstacle start end Convex Hull Convex Hull
One of the most important problems is Computational Geometry is to find an efficient way to decide, given a subdivision of E n and a point P, in which.
UNC Chapel Hill M. C. Lin Polygon Triangulation Chapter 3 of the Textbook Driving Applications –Guarding an Art Gallery –3D Morphing.
9/12/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2006 Triangulations and Guarding Art Galleries II Carola Wenk.
I. The Problem of Molding Does a given object have a mold from which it can be removed? object not removable mold 1 object removable Assumptions The object.
1 Voronoi Diagrams. 2 Voronoi Diagram Input: A set of points locations (sites) in the plane.Input: A set of points locations (sites) in the plane. Output:
Overlay of Two Subdivisions
Convex Sets & Concave Sets A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R.
ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis.
9/5/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2006 Plane Sweep Algorithms and Segment Intersection Carola Wenk.
Intersections. Intersection Problem 3 Intersection Detection: Given two geometric objects, do they intersect? Intersection detection (test) is frequently.
Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,
CS138A Line Segment Intersection Peter Schröder.
17. Computational Geometry Chapter 7 Voronoi Diagrams.
N Line Segments Problem Given a set of n line segments in the plane, determine whether or not any intersection exists. It is not required to output all.
Point Location Computational Geometry, WS 2007/08 Lecture 5 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für.
Lecture 6: Point Location Computational Geometry Prof. Dr. Th. Ottmann 1 Point Location 1.Trapezoidal decomposition. 2.A search structure. 3.Randomized,
Voronoi Diagrams.
Line Segment Intersection Computational Geometry, WS 2006/07 Lecture 3 – Part II Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik.
Lecture 2 Line Segment Intersection Computational Geometry Prof.Dr.Th.Ottmann 1 Line Segment Intersection Motivation: Computing the overlay of several.
Brute-Force Triangulation
Advanced Algorithm Design and Analysis (Lecture 9) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Geometric Intersection Determining if there are intersections between graphical objects Finding all intersecting pairs Brute Force Algorithm Plane Sweep.
9/7/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2006 Triangulations and Guarding Art Galleries Carola Wenk.
UNC Chapel Hill M. C. Lin Point Location Reading: Chapter 6 of the Textbook Driving Applications –Knowing Where You Are in GIS Related Applications –Triangulation.
Triangulating a monotone polygon
UNC Chapel Hill M. C. Lin Line Segment Intersection Chapter 2 of the Textbook Driving Applications –Map overlap problems –3D Polyhedral Morphing.
2/19/15CMPS 3130/6130 Computational Geometry1 CMPS 3130/6130 Computational Geometry Spring 2015 Voronoi Diagrams Carola Wenk Based on: Computational Geometry:
CMPS 3130/6130 Computational Geometry Spring 2015
Computational Geometry: Intersection Search Joseph S. B. Mitchell Stony Brook University.
UNC Chapel Hill M. C. Lin Computing Voronoi Diagram For each site p i, compute the common inter- section of the half-planes h(p i, p j ) for i  j, using.
A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R. Otherwise, it is called concave.
February 17, 2005Lecture 6: Point Location Point Location (most slides by Sergi Elizalde and David Pritchard)
9/8/10CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2010 Triangulations and Guarding Art Galleries Carola Wenk.
1/20/15CMPS 3130/6130 Computational Geometry1 CMPS 3130/6130 Computational Geometry Spring 2015 Plane Sweep Algorithms I Carola Wenk.
Polygon Triangulation
Computational Geometry
UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query.
CMPS 3130/6130 Computational Geometry Spring 2017
3. Polygon Triangulation
Computational Geometry
CMPS 3130/6130 Computational Geometry Spring 2017
CMPS 3130/6130 Computational Geometry Spring 2017
Orthogonal Range Searching and Kd-Trees
Computational Geometry Capter:1-2.1
Convex Sets & Concave Sets
I. The Problem of Molding
Overlay of Two Subdivisions
Algorithms and Data Structures Lecture XIV
Presentation transcript:

Two Segments Intersect? One method: solve for the intersection point of the two lines containing the two line segments, and then check whether this point lies on both segments. In practice, the two input segments often do not intersect. Stage 1: quick rejection if their bounding boxes do not intersect x < x  x > x  y < y  y > y 4 1 3 2 4 1 3 2 if and only if L right of M? L left of M? L above M? L below M? (x , y ) (x , y ) 2 2 (x , y ) 1 1 3 3 4 4 bounding boxes L M Case 1: bounding boxes do not intersect; neither will the segments.

Bounding Box Case 2: Bounding boxes intersect; the segments may or may not intersect. Needs to be further checked in Stage 2.

Stage 2 Two line segments do not intersect if and only if one segment lies entirely to one side of the line containing the other segment. p 1 2 3 4 ( p  p )  ( p  p ) and ( p  p )  ( p  p ) are both positive! 3 2 1 2 4 2 1 2

Necessary and Sufficient Condition Two line segments intersect iff each of the two pairs of cross products below have different signs (or one cross product in the pair is 0). 3 1 // the line through p p // intersects p p . 4 2 (p  p )  ( p  p ) and ( p  p )  ( p  p ) 1 4 3 4 2 4 3 4 3 2 1 2 4 2 1 2 3 // the line through p p // intersects p p . 4 1 2 p 1 2 3 4 p 1 2 3 4

n Line Segments Input: a set of n line segments in the plane. Output: all intersections and for each intersection the involved segments. .

A Brute-Force Algorithm Simply take each pair of segments, and check if they intersect. If so, output the intersection. Running time (n ). 2 Nevertheless, sparse distribution in practice: Most segments do not intersect, or if they do, only with a few other segments. Need a faster algorithm that deals with such situations!

The Sweeping Algorithm Avoid testing pairs of segments that are far apart. Idea: imagine a vertical sweep line passes through the given set of line segments, from left to right. Sweep line

Assumption on Non-degeneracy No segment is vertical. // the sweep line always hits a segment at // a point. If ≥ 1 vertical segment, imagine all segments are rotated clockwise by a tiny angle and then test for intersection. This means: For each vertical segment, we will consider its lower endpoint before upper point.

Sweep Line Status The set of segments intersecting the sweep line. It changes as the sweep line moves, but not continuously. endpoints Updates of status happen only at event points. intersections A G C T event points

Ordering Segments A total order over the segments that intersect the current position of the sweep line: B > C > D (A and E not in the ordering) B A E C C > D (B drops out of the ordering) D At an event point, the sequence of segments changes:  Update the status.  Detect the intersections.

Status Update (1) Event point is the left endpoint of a segment. K O L A new segment L intersecting the sweep line K O L Check if L intersects with the segment above (K) and the segment below (M). M Intersection(s) are new event points. N new event point K, M, N K, L, M, N

Status Update (2) Event point is an intersection. K O L M N The two intersecting segments (L and M) change order. K O L Check intersection with new neighbors (M with O and L with N). M Intersection(s) are new event points. N O, L, M, N O, M, L, N

Status Update (3) Event point is a lower endpoint of a segment. K O L The two neighbors (O and L) become adjacent. K O L Check if they (O and L) intersect. M Intersection is new event point. N O, M, L, N O, L, N

Correctness Invariant holding at any time during the plane sweep. All intersection points to the left of the sweep line have been computed correctly. The correctness of the algorithm thus follows.

Data Structure for Event Queue Ordering of event points: by x-coordinates by y-coordinates in case of a tie in x-coordinates. Supports the following operations on a segment s.  fetching the next event // O(log m)  inserting an event // O(log m) Every event point p is stored with all segments starting at p. Data structure: balanced binary search tree (e.g., red-black tree). m = #event points in the queue

Data Structure for Sweep-line Status Describes the relationships among the segments intersected by the sweep line. Use a balanced binary search tree T to support the following operations on a segment s. Insert(T, s) Delete(T, s) Above(T, s) // segment immediately above s Below(T, s) // segment immediately below s e.g, Red-black trees, splay trees (key comparisons replaced by cross-product comparisons). O(log n) for each operation.

An Example O M N N K O L L N M K K L O The bottom-up order of the segments correspond to the left-to-right order of the leaves in the tree T.  Each internal node stores the segment from the rightmost leaf in its left subtree.

Additional Operation O M N N K O p L L N M K K L L O Searching for the segment immediately below some point p on the sweep line.  Descend binary search tree all the way down to a leaf.  Outputs either this leaf or the leaf immediately to its left . O(log n) time

The Algorithm FindIntersections(S) Input: a set S of line segments Ouput: all intersection points and for each intersection the segment containing it. 1. Q  // initialize an empty event queue 2. Insert the segment endpoints into Q // store with every left endpoint // the corresponding segments 3. T  // initialize an empty status structure while Q   do extract the next event point p 6. Q  Q – {p} 7. HandleEventPoint(p)

Handling Event Points T: Status updates (1) – (3) presented earlier. D Degeneracy: several segments are involved in one event point (tricky). D A G E H C A B C D G E H l T: p G C H A B (a) Delete D, E, A, C (b) Insert B, A, C

The Code of Event Handling HandleEventPoint(p) 1. L(p)  { segments with left endpoint p } // they are stored with p 2. S(p) { segments containing p} // all adjacent in T and found by a search 3. R(p) = { segments with right endpoint p} // L(p)  S(p) 4. C(p) = { segments with p in interior } // C(p)  S(p) if | L  R  C | > 1 then report p as an intersection along with L, R, C T  T – (R  C ) T  T  (L  C) // order as intersected by sweep line just to the // right of p. segments in C(p) have order reversed. 9. if L  C =  // right endpoint only 10. then let s and s be the neighbors right below and above p in T 11. FindNewEvent(s , s , p) 12. else s  lowest segment in L  C 13. s  segment right below s 14. FindNewEvent(s , s, p) 15. s  highest segment in L  C 16. s  segment right above s 17. FindNewEvent(s, s , p) b a b a s a s b p b s´ s a b a

Finding New Event FindNewEvent(s , s , p) if s and s intersect to the right of p // sweep line position then insert the intersection point as an event in Q l r r l

Correctness Lemma Algorithm FindIntersections computes all interesections and their containing segments correctly. Proof By induction. Let p be an intersection point and assume all intersections with a higher priority have been computed correctly. p is an endpoint.  stored in the event queue Q. All involved are determined correctly. L(p) is also stored in Q. R(p) and C(p) are stored in T and will be found. p is not an endpoint. We show that p will be inserted into Q. A B p q All involved segments have p in interior. Order them by polar angle. Let A and B be neighboring segments. There exists event point q < p after which A and B become adjacent..  By induction, q was handled corrected and p is detected and stored in Q

Output Sensitivity An algorithm is output sensitive if its running time is sensitive to the size of the output. The plane sweeping algorithm is output sensitive. running time O((n+k) log n) output size (intersections and their containing segments) But one intersection may consist of (n) segments. When this happens, running time becomes . Not tight! – the total number of intersections may still be (n).

A Tighter Bound The running time is O(nlog n + I log n). # intersections Idea in analysis all time cost is spent on maintaining the data structures: the event queue Q and the status structure tree T. FindIntersections(S) 1. Q  // initialize an empty event queue Insert the segment endpoints into Q // balanced binary search tree O(n log n) T  // initialize an empty status structure while Q   do extract the next event point p 6. Q  Q – {p} // deletion from Q takes time O(log n). 7. HandleEventPoint(p) // 1 or 2 calls to FindNewEvent // each call may result in an insertion into Q: O(log n) . // each operation on T – insertion, deletion, neighbor // finding – takes time O(log n) . // #operations on T is (|L(p)R(p) C(p)|)

#Tree Operations Let Then the running time is O((m+n) log n). Claim m = O(n+I). Proof View the set of segments as a planar graph. | L(p)  R(p)  C(p)|  deg(p)  m   deg(p) Let n = #edges, n = #vertices. e v Every edge contributes one each to the degree of its two vertices. So  deg(p)  2 n.  m  2 n e  n = O(n ) // Euler’s equation v e But n  2n + I. The conclusion follows. v

Storage The tree T stores every segment once. O(n) The size of the event queue Q: O(n+I). Reduce the storage to O(n). Store intersections among adjacent segments. Delete those of segments that stop being adjacent. Theorem All I intersections of n line segments in the plane can be reported in O((n+I) log n) time and O(n) space.