· Lecture Line Segment Intersection

INSTITUT FUR¨ THEORETISCHE INFORMATIK · FAKULTAT¨ FUR¨ INFORMATIK

Tamara Mchedlidze · Darren Strash 26.10.2015

1 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Aside: Organizational Items

2 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Overlaying Map Layers Example: Given two different map layers whose intersection is of interest. Land use

+ Precipitation =

Map combining themes

3 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Overlaying Map Layers Example: Given two different map layers whose intersection is of interest. Land use

+ Precipitation =

Map combining themes

Regions are polygons Polygons are line segments Calculate all line segment intersections Compute regions

3 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Problem Formulation

Given: Set S = {s1, . . . , sn} of line segments in the plane Output: all intersections of two or more line segments for each intersection, the line segments involved.

4 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Problem Formulation

Given: Set S = {s1, . . . , sn} of line segments in the plane Output: all intersections of two or more line segments for each intersection, the line segments involved. Def: Line segments are closed

4 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Problem Formulation

Given: Set S = {s1, . . . , sn} of line segments in the plane Output: all intersections of two or more line segments for each intersection, the line segments involved. Def: Line segments are closed

Discussion: – How can you solve this problem naively? – Is this already optimal? – Are their better approaches?

4 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example Events

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

sweep line

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection The Sweep-Line Method: An Example

5 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q `

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q ` Store events by ≺ in a balanced binary search → e.g., AVL tree, red-black tree, . . .

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q ` Store events by ≺ in a balanced → e.g., AVL tree, red-black tree, . . . Operations insert, delete and nextEvent in O(log |Q|) time

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q ` Store events by ≺ in a balanced binary search tree → e.g., AVL tree, red-black tree, . . . Operations insert, delete and nextEvent in O(log |Q|) time

2.) Sweep-Line Status T `

Stores ` cut lines ordered from left to right

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q ` Store events by ≺ in a balanced binary search tree → e.g., AVL tree, red-black tree, . . . Operations insert, delete and nextEvent in O(log |Q|) time

2.) Sweep-Line Status T `

Stores ` cut lines ordered from left to right Required operations insert, delete, findNeighbor

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Data Structures 1.) Event Queue Q

define p ≺ q ⇔def. yp > yq ∨ (yp = yq ∧ xp < xq) p q ` Store events by ≺ in a balanced binary search tree → e.g., AVL tree, red-black tree, . . . Operations insert, delete and nextEvent in O(log |Q|) time

2.) Sweep-Line Status T `

Stores ` cut lines ordered from left to right Required operations insert, delete, findNeighbor This is also a balanced binary search tree with line segments stored in the leaves!

6 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection FindIntersections(S) Input: Set S of line segments Output: Set of all intersection points and the line segments involved Q ← ∅; T ← ∅ foreach s ∈ S do Q.insert(upperEndPoint(s)) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p)

7 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm FindIntersections(S) Input: Set S of line segments Output: Set of all intersection points and the line segments involved Q ← ∅; T ← ∅ foreach s ∈ S do Q.insert(upperEndPoint(s)) What happens Q.insert(lowerEndPoint(s)) with duplicates? while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p)

7 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm FindIntersections(S) Input: Set S of line segments Output: Set of all intersection points and the line segments involved Q ← ∅; T ← ∅ foreach s ∈ S do Q.insert(upperEndPoint(s)) What happens Q.insert(lowerEndPoint(s)) with duplicates? while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p) This is the core of the algorithm!

7 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm handleEvent(p) U(p) ← Line segments with p as upper endpoint L(p) ← Line segments with p as lower endpoint C(p) ← Line segments with p as interior point if |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then return p and U(p) ∪ L(p) ∪ C(p) remove L(p) ∪ C(p) from T add U(p) ∪ C(p) to T if U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in T Q ← check if sl and sr intersect below p else //s0 and s00 leftmost and rightmost line segment in U(p) ∪ C(p) 0 Q ← check if sl and s intersect below p 00 Q ← check if sr and s intersect below p

8 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm handleEvent(p) U(p) ← Line segments with p as upper endpointStored with p in Q L(p) ← Line segments with p as lower endpoint C(p) ← Line segments with p as interior point if |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then return p and U(p) ∪ L(p) ∪ C(p) remove L(p) ∪ C(p) from T add U(p) ∪ C(p) to T if U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in T Q ← check if sl and sr intersect below p else //s0 and s00 leftmost and rightmost line segment in U(p) ∪ C(p) 0 Q ← check if sl and s intersect below p 00 Q ← check if sr and s intersect below p

8 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm handleEvent(p) U(p) ← Line segments with p as upper endpointStored with p in Q L(p) ← Line segments with p as lower endpoint C(p) ← Line segments with p as interior point Neighbors in T if |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then return p and U(p) ∪ L(p) ∪ C(p) remove L(p) ∪ C(p) from T add U(p) ∪ C(p) to T if U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in T Q ← check if sl and sr intersect below p else //s0 and s00 leftmost and rightmost line segment in U(p) ∪ C(p) 0 Q ← check if sl and s intersect below p 00 Q ← check if sr and s intersect below p

8 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Algorithm handleEvent(p) U(p) ← Line segments with p as upper endpointStored with p in Q L(p) ← Line segments with p as lower endpoint C(p) ← Line segments with p as interior point Neighbors in T if |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then return p and U(p) ∪ L(p) ∪ C(p) remove L(p) ∪ C(p) from T Remove and insert add U(p) ∪ C(p) to T reverses order in C(p) if U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in T Q ← check if sl and sr intersect below p else //s0 and s00 leftmost and rightmost line segment in U(p) ∪ C(p) 0 Q ← check if sl and s intersect below p 00 Q ← check if sr and s intersect below p

8 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2

U(p) = {s2} L(p) = C(p) =

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2

U(p) = {s2} L(p) = C(p) =

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2

U(p) = {s2} L(p) = {s4, s5} C(p) = {s1, s3}

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2

U(p) = {s2} L(p) = {s4, s5} C(p) = {s1, s3}

Report (p, {s1, s2, s3, s4, s5})

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1

s2 s2

s3 s1 U(p) = {s2} s s s s L(p) = {s4, s5} 6 2 1 7 C(p) = {s1, s3} s6 s3 Delete L(p) ∪ C(p); add U(p) ∪ C(p)

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection What Happens Exactly?

S T s1 s1 s4 s5 s3 s3 s6 s5 s6 s4 s3 s7 p s7 s6 s5 s4 s1 p0

s2 s2

s3 s1 U(p) = {s2} s s s s L(p) = {s4, s5} 6 2 1 7 00 C(p) = {s1, s3} s sr s6 s3 0 0 sl s Add event p = s1 × s7 in Q

9 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Correctness Lemma 1: Algorithm FindIntersections finds all intersection points and the line segments involved

10 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Correctness Lemma 1: Algorithm FindIntersections finds all intersection points and the line segments involved Proof: Induction on the number of events processed.

Let p be an intersection point and all intersection points q ≺ p are already correctly computed.

Case 1: p is a line segment endpoint p was inserted in Q U(p) stores p L(p) and C(p) are in T

10 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Correctness Lemma 1: Algorithm FindIntersections finds all intersection points and the line segments involved Proof: Induction on the number of events processed.

Let p be an intersection point and all intersection points q ≺ p are already correctly computed.

Case 1: p is a line segment endpoint p was inserted in Q U(p) stores p L(p) and C(p) are in T Case 2: p is not a line segment endpoint Consider why p must be in Q!

10 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis FindIntersections(S) Input: Set S of line segments Output: Set of all intersections with their line segments Q ← ∅; T ← ∅ foreach s ∈ S do Q.insert(upperEndPoint(s)) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p)

11 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis FindIntersections(S) Input: Set S of line segments Output: Set of all intersections with their line segments Q ← ∅; T ← ∅ O(1) foreach s ∈ S do Q.insert(upperEndPoint(s)) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p)

11 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis FindIntersections(S) Input: Set S of line segments Output: Set of all intersections with their line segments Q ← ∅; T ← ∅ O(1) foreach s ∈ S do Q.insert(upperEndPoint(s)) O(n log n) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() Q.deleteEvent(p) handleEvent(p)

11 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis FindIntersections(S) Input: Set S of line segments Output: Set of all intersections with their line segments Q ← ∅; T ← ∅ O(1) foreach s ∈ S do Q.insert(upperEndPoint(s)) O(n log n) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() O(log |Q|) Q.deleteEvent(p) handleEvent(p)

11 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis FindIntersections(S) Input: Set S of line segments Output: Set of all intersections with their line segments Q ← ∅; T ← ∅ O(1) foreach s ∈ S do Q.insert(upperEndPoint(s)) O(n log n) Q.insert(lowerEndPoint(s)) while Q= 6 ∅ do p ← Q.nextEvent() O(log |Q|) Q.deleteEvent(p) ? handleEvent(p)

11 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Running-Time Analysis handleEvent(p) U(p) ← Line segments with p as upper endpoint L(p) ← Line segments with p as lower endpoint C(p) ← Line segments with p as interior point if |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then return p and U(p) ∪ L(p) ∪ C(p) remove L(p) ∪ C(p) from T add U(p) ∪ C(p) to T if U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in T Q ← check if sl and sr intersect below p else //s0 and s00 leftmost and rightmost line segment in U(p) ∪ C(p) 0 Q ← check if sl and s intersect below p 00 Q ← check if sr and s intersect below p

Lemma 2:Algorithm FindIntersections has running time O(n log n + I log n), where I is the number of intersection points. 12 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Summary Thm 1:Let S be a set of n line segments in the plane. Then we can compute intersections in S together with the involved line segments in O((n + I) log n) time and O(?) space.

13 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Summary Thm 1:Let S be a set of n line segments in the plane. Then we can compute intersections in S together with the involved line segments in O((n + I) log n) time and O(?) space. Proof: Correctness X Running time X Space

13 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Summary Thm 1:Let S be a set of n line segments in the plane. Then we can compute intersections in S together with the involved line segments in O((n + I) log n) time and O(?) space. Proof: Correctness X Consider how much space the Running time X data structures need! Space

13 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Summary Thm 1:Let S be a set of n line segments in the plane. Then we can compute intersections in S together with the involved line segments in O((n + I) log n) time and O(n) space. Proof: Correctness X Consider how much space the Running time X data structures need! Space – T has at most n elements – Q has at most O(n + I) elements – reduction of Q to O(n) space: an exercise

13 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one?

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one? No, because if I ∈ Ω(n2) then the algorithm has running time O(n2 log n).

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one? No, because if I ∈ Ω(n2) then the algorithm has running time O(n2 log n).

Can we do better?

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one? No, because if I ∈ Ω(n2) then the algorithm has running time O(n2 log n).

Can we do better? Yes, in Θ(n log n + I) time and Θ(n) space [Balaban, 1995].

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one? No, because if I ∈ Ω(n2) then the algorithm has running time O(n2 log n).

Can we do better? Yes, in Θ(n log n + I) time and Θ(n) space [Balaban, 1995].

How does this solve the map overlay problem?

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection Discussion Is the Sweep-Line Algorithm always better than the naive one? No, because if I ∈ Ω(n2) then the algorithm has running time O(n2 log n).

Can we do better? Yes, in Θ(n log n + I) time and Θ(n) space [Balaban, 1995].

How does this solve the map overlay problem? Using an appropriate (doubly-connected edgelist) for planar graphs we can compute in O((n + I) log n) time the overlay of two maps. (Details in Ch. 2.3 of the book)

14 Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection