line segment intersection - kit...calculate all line segment intersections compute regions 3 dr....
TRANSCRIPT
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Tamara Mchedlidze · Darren Strash
Computational Geometry · Lecture
INSTITUT FUR THEORETISCHE INFORMATIK · FAKULTAT FUR INFORMATIK
Line Segment Intersection
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 isof 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 isof interest.
Land use
Precipitation
Map combining themes
Regions are polygonsPolygons are line segmentsCalculate all line segment intersectionsCompute 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 segmentsfor 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 segmentsfor 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 segmentsfor each intersection, the line segments involved.
Def: Line segments are closed
Discussion:– How can you solve this problemnaively?– 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 Qdefine 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 Qdefine 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, . . .
6
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Data Structures
1.) Event Queue Qdefine 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
6
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Data Structures
1.) Event Queue Qdefine 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 Qdefine 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 Qdefine 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 inthe leaves!
6
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Algorithm
FindIntersections(S)
Input: Set S of line segmentsOutput: Set of all intersection points and the line segments
involvedQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← 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 segmentsOutput: Set of all intersection points and the line segments
involvedQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
What happenswith duplicates?
7
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Algorithm
FindIntersections(S)
Input: Set S of line segmentsOutput: Set of all intersection points and the line segments
involvedQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
What happenswith duplicates?
This is the core of thealgorithm!
7
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Algorithm
handleEvent(p)
U(p)← Line segments with p as upper endpointL(p)← Line segments with p as lower endpointC(p)← Line segments with p as interior pointif |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then
return p and U(p) ∪ L(p) ∪ C(p)
remove L(p) ∪ C(p) from Tadd U(p) ∪ C(p) to Tif U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in TQ ← check if sl and sr intersect below p
else //s′ and s′′ leftmost and rightmost line segment in U(p) ∪ C(p)Q ← check if sl and s′ intersect below pQ ← 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 endpointL(p)← Line segments with p as lower endpointC(p)← Line segments with p as interior pointif |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then
return p and U(p) ∪ L(p) ∪ C(p)
remove L(p) ∪ C(p) from Tadd U(p) ∪ C(p) to Tif U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in TQ ← check if sl and sr intersect below p
else //s′ and s′′ leftmost and rightmost line segment in U(p) ∪ C(p)Q ← check if sl and s′ intersect below pQ ← check if sr and s′′ intersect below p
Stored with p in Q
8
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Algorithm
handleEvent(p)
U(p)← Line segments with p as upper endpointL(p)← Line segments with p as lower endpointC(p)← Line segments with p as interior pointif |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then
return p and U(p) ∪ L(p) ∪ C(p)
remove L(p) ∪ C(p) from Tadd U(p) ∪ C(p) to Tif U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in TQ ← check if sl and sr intersect below p
else //s′ and s′′ leftmost and rightmost line segment in U(p) ∪ C(p)Q ← check if sl and s′ intersect below pQ ← check if sr and s′′ intersect below p
Stored with p in Q
Neighbors in T
8
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Algorithm
handleEvent(p)
U(p)← Line segments with p as upper endpointL(p)← Line segments with p as lower endpointC(p)← Line segments with p as interior pointif |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then
return p and U(p) ∪ L(p) ∪ C(p)
remove L(p) ∪ C(p) from Tadd U(p) ∪ C(p) to Tif U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in TQ ← check if sl and sr intersect below p
else //s′ and s′′ leftmost and rightmost line segment in U(p) ∪ C(p)Q ← check if sl and s′ intersect below pQ ← check if sr and s′′ intersect below p
Stored with p in Q
Neighbors in T
Remove and insertreverses order in C(p)
8
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) =C(p) =
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) =C(p) =
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) = s4, s5C(p) = s1, s3
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) = s4, s5C(p) = s1, s3
Report (p, s1, s2, s3, s4, s5)
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) = s4, s5C(p) = s1, s3 s6
s7
s2
s3
s6
s1
s3
s2 s1
Delete L(p) ∪ C(p); add U(p) ∪ C(p)
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
What Happens Exactly?
s1
s2
s3
s4
s5s6
s7 s6 s5 s4 s1
s3 s7
s1
s5
s6 s4
s3
T
p
U(p) = s2L(p) = s4, s5C(p) = s1, s3 s6
s7
s2
s3
s6
s1
s3
s2 s1
Add event p′ = s1 × s7 in Q s′
s′′ sr
sl
p′
S
9
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Correctness
Lemma 1:Algorithm FindIntersections finds all intersectionpoints 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 intersectionpoints and the line segments involved
Proof:Induction on the number of events processed.
Let p be an intersection point and all intersection points q ≺ pare already correctly computed.
Case 1: p is a line segment endpointp was inserted in QU(p) stores pL(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 intersectionpoints and the line segments involved
Proof:Induction on the number of events processed.
Let p be an intersection point and all intersection points q ≺ pare already correctly computed.
Case 1: p is a line segment endpointp was inserted in QU(p) stores pL(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 segmentsOutput: Set of all intersections with their line segmentsQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← 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 segmentsOutput: Set of all intersections with their line segmentsQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
O(1)
11
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Running-Time Analysis
FindIntersections(S)
Input: Set S of line segmentsOutput: Set of all intersections with their line segmentsQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
O(1)
O(n log n)
11
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Running-Time Analysis
FindIntersections(S)
Input: Set S of line segmentsOutput: Set of all intersections with their line segmentsQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
O(1)
O(n log n)
O(log |Q|)
11
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Running-Time Analysis
FindIntersections(S)
Input: Set S of line segmentsOutput: Set of all intersections with their line segmentsQ ← ∅; T ← ∅foreach s ∈ S doQ.insert(upperEndPoint(s))Q.insert(lowerEndPoint(s))
while Q 6= ∅ dop← Q.nextEvent()Q.deleteEvent(p)handleEvent(p)
O(1)
O(n log n)
O(log |Q|) ?
11
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Running-Time Analysis
Lemma 2:Algorithm FindIntersections has running timeO(n log n + I log n), where I is the number ofintersection points.
handleEvent(p)
U(p)← Line segments with p as upper endpointL(p)← Line segments with p as lower endpointC(p)← Line segments with p as interior pointif |U(p) ∪ L(p) ∪ C(p)| ≥ 2 then
return p and U(p) ∪ L(p) ∪ C(p)
remove L(p) ∪ C(p) from Tadd U(p) ∪ C(p) to Tif U(p) ∪ C(p) = ∅ then //sl and sr, neighbors of p in TQ ← check if sl and sr intersect below p
else //s′ and s′′ leftmost and rightmost line segment in U(p) ∪ C(p)Q ← check if sl and s′ intersect below pQ ← check if sr and s′′ intersect below p
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. Thenwe can compute intersections in S together with theinvolved line segments in O((n + I) log n) time andO(?) 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. Thenwe can compute intersections in S together with theinvolved line segments in O((n + I) log n) time andO(?) space.
Proof:Correctness XRunning time XSpace
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. Thenwe can compute intersections in S together with theinvolved line segments in O((n + I) log n) time andO(?) space.
Proof:Correctness XRunning time XSpace
Consider how much space thedata structures need!
13
Dr. Tamara Mchedlidze · Dr. Darren Strash · Computational Geometry Lecture Line Segment Intersection
Summary
Proof:Correctness XRunning time XSpace
– T has at most n elements– Q has at most O(n + I) elements– reduction of Q to O(n) space: an exercise
Consider how much space thedata structures need!
Thm 1:Let S be a set of n line segments in the plane. Thenwe can compute intersections in S together with theinvolved line segments in O((n + I) log n) time andO(n) space.
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 timeO(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 timeO(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 timeO(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 timeO(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 timeO(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 data structure (doubly-connected edgelist) forplanar graphs we can compute in O((n + I) log n) time the overlay oftwo maps.(Details in Ch. 2.3 of the book)
14