Line Segment Intersection Computational Geometry, WS 2007/08 Lecture 3 – Supplementary Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann2 Line Segment Intersection: Additions Implementation of the event queue by a heap: The complexity of heap construction Iso-oriented line segments Intersection counting Line segment intersection by Divide & Conquer Polygon intersection
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann3 Heaps Definition. A sequence F = (k 1, k 2,..., k n ) of keys is a (Max-) Heap, iff for all i {1, 2, …, n/2} : k i ≥ k 2i und k i ≥ k 2i+1. This requirement for an array can be interpreted as a condition on a complete binary tree: Level i has 2 i nodes (except, eventually, the last level) Node i has node 2i as ist left and node 2i + 1 as its right successor and node i/2 as ist predecessor (if they exist)
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann4 Heap Construction Level, #nodes, #comp./exchanges k levels, total # of nodes = 2 k+1 -1
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann5 Heap Construction Heap construction by iterative trickling down: ² k = 32 k nodes Total number of nodes: n = 2 k Total number of key comparisons and exchanges:.
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann6 Complexity of Heap Construction Total complexity = 2 0 2k (k-1) (k-2) + … + 2 (k-1 ) 2 = 2 S(k), where S(k) = 2 0 k (k-1) (k-2) + … + 2 (k-1) 1 = 2 (n – k – 1) = O(n)
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann7 Intersections of iso-oriented Line Segments Report all pairs of intersecting segments Count the number of intersecting segments
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann8 Iso-oriented Line Segments Report all pairs of intersecting segments: A sweep-line algorithm can solve this problem in optimal time O(n log n + k) and space O(n), k = # of intersections!
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann9 Iso-oriented Line Segments Count all pairs of intersecting segments: A sweep-line algorithm can solve this problem in optimal time O(n log n) and space O(n)!
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann10 A Divide and Conquer Algorithm Report all pairs of intersecting segments
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann11 How to divide the input set A separate representation of segments allows to divide the input set. A B C D E A. B. C. D. E..A.D.B.C.E
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann12 ReportCuts(S) Input:Set S of vertical segments and endpoints of horizontal segments. Output: All pairs of intersecting vertical segments and those horizontal segments which are represented by at least one endpoint in S. 1. Divide if |S| > 1 then divide S by a vertical line G into two sets S 1 (to the left of G) and S 2 (to the right of G) of equal size else S contains no intersections
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann13 ReportCuts A B C D E A D B C E S S1S1 S2S2 1. Divide 2. Conquer ReportCuts(S 1 ); ReportCuts(S 2 )
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann14 ReportCuts 3. Merge: ??? Different possibilities for intersections of a horizontal segment in S 1 Case 1: both endpoints in S 1 h S1S1 S2S2
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann15 ReportCuts Case 2: only one endpoint of h in S1 2 a) right endpoint in S1 h S1S1 S2S2
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann16 ReportCuts 2 b) left endpoint of h in S 1 h S1S1 right endpoint in S 2 h S1S1 right endpoint not in S 2 S2S2 S2S2
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann17 ReportCuts(S) 3. Merge: Report intersections between vertical segments in S 2 and those horizontal segments in S 1, for which the left endpoint is in S 1 and the right endpoint is neither in S 1 nor in S 2 Analogously for S 1 S1S1 S2S2
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann18 Implemention Set S L(S): y-coordinats of all left endpoints in S which have no right partner in S R(S): y-coordinats of all right endpoints in S which have no left partner in S V(S): y-intervals of vertical segments in S
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann19 Base Cases S contains only one element s Case 1: s = (x,y) is left endpoint L(S) = {y} R(S) = V(S) = Case 2: s = (x,y) is right endpoint L (S)= R(S) = {y} V(S) = Case 3: s = (x,y 1,y 2 ) is vertical segment L(S) = R(S) = V(S) = {[y 1,y 2 ]}
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann20 Merge Step Assume L(S i ), R(S i ), V(S i ) i=1,2 are known and S = S 1 S 2 L(S) = R(S) = V(S) = L,R: sorted according to increasing y-coordinats linked lists V: sorted according to increasing lower endpoints linked list
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann21 Reporting of Intersections V(S 2 ) h3h3 h2h2 h1h1 L(S 1 )
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann22 Runtime Input (vertical segments, left/right endpoints of horizontal segments) has to be sorted initially and is stored in an array. Divide-and-Conquer: T(n) = 2T(n/2) + an + output-size T(1) = O(1) O(n log n + k) k = #intersections
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann23 Polygon Intersection by Line Sweep A B C D E F G
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann24 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q Sweep direction
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann25 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann26 GEADGEAD Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D Q1 Reported Intersections 1. Q1 (E--A) Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann27 GAEDGAED Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Q2 Q3 Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) Q3 Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann28 GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) Q3 GAEDGAED Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann29 GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) Q3 GAEDGAED (E = F) Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann30 G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) Q3 GAEDGAED Status-structure T:
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann31 G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) Q3 GAEDGAED Status-structure T:AGBCDFAGBCDF Q4
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann32 ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q Sweep direction A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) Q3 GAEDGAED Status-structure T: Q5 Q4 Q5
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann33 ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) Q3 GAEDGAED Status-structure T: Q4 Q5 ABGCDFABGCDF
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann34 ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) 6. Q6 (G--D) Q3 GAEDGAED Status-structure T: Q4 Q5 ABGCDFABGCDF CGDFCGDF Q6
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann35 CDGFCDGF CGDFCGDF ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) 6. Q6 (G--D) Q3 GAEDGAED Status-structure T: Q4 Q5 ABGCDFABGCDF Q6
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann36 CDGFCDGF CGDFCGDF ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) 6. Q6 (G--D) Q3 GAEDGAED Status-structure T: Q4 Q5 ABGCDFABGCDF Q6 C D G F
Computational Geometry, WS 2007/08 Prof. Dr. Thomas Ottmann37 CDGFCDGF CGDFCGDF ABGCDFABGCDF AGBCDFAGBCDF G A B C D F GADFGADF GADEGADE Q2 Q1 Polygon Intersection by Line Sweep A B C D E F G Event-queue Q A D GEADGEAD Reported Intersections 1. Q1 (E--A) 2. Q2 (G--A) 3. Q3 (E--D) 4. Q4 (G--B) 5. Q5 (G--C) 6. Q6 (G--D) Q3 GAEDGAED Status-structure T: Q4 Q5 ABGCDFABGCDF Q6 C D G F CDCD