Upload
pauline-lloyd
View
216
Download
0
Embed Size (px)
Citation preview
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 the intersections.
A Brute-Force Algorithm
Simply take each pair of segments, and check if they intersect.If so, output yes to the original problem; otherwise, output no.
Running time (n ). 2
Most segments do not intersect, or if they do, only with a few other segments.
Need an faster algorithm for testing in such situations!
Nevertheless, sparse distribution in practice:
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.
Sweepline
Assumptions on Non-degeneracy
1. No segment is vertical. // the sweep line always hits a segment at // a point.
If ≥ 1 vertical segment, rotate all segmentsby a small angle and then test for intersection.
2. No three segments are concurrent.
Exercise 33.2-8 on how to deal with the violating case.
Dealing with degeneracies are often the most laborious part of implementing geometric algorithms and proving their correctness.
Ordering Segments
A total order over the segments that intersect the current position of the sweep line:
a
b
c
d
eb > c > d(a and e are outof the ordering)
Such a total order T is called the sweep-line status.
Sweep-line Status
Describes the relationships among the segments intersected by the sweep line.
Supports 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
Red-black tree implementation (key comparisons replacedby cross-product comparisons).
O(lg n) for each operation.
Sweeping around an Intersection
The order of the two intersecting segments gets reversed.
a
b
c
d
e
f
before afterd < f < c < b d < c < f < b
Event-point Schedule
The sweeping algorithm does something only when it reachesan endpoint.
Sort the segment endpoints by increasing x-coordinate and proceed from left to right.
A sequence of x-coordinates, in increasing order.
Where will the sweep line make intermediate stops?
An Example
a
b
c
d e
f
a
Intersect!
ab
acb
dacb
dcb
edcb
edb
Sweep-linestatus:
The Sweeping Algorithm
Any-Segments-Intersect(S) // a set S of line segments T = { } // total order of segments intersecting the sweep line sort the endpoints of the segments from left to right for each point p in the sorted list
do if p is the left endpoint of a segment s then Insert(T, s) if (Above(T, s) exists and intersects s)
or (Below(T, s) exists and intersects s) then return true if p is the right endpoint of a segment s then if both Above(T, s) and Below(T, s) exist
and they intersect then return true
Delete (T, s) return false
Above(T, s)
Below(T, s)
sp
Above(T, s)
Below(T, s)
ps
Running Time
Total time O(n lg n).
Sorting of line segments takes O(n lg n) time.
There are 2n event points, each costs O(lg n) on updating thesweep-line status.