110 likes | 122 Views
Determine if any intersection exists among a set of line segments in the plane using a Sweeping Algorithm, avoiding brute-force testing of all segment pairs. Implement a sweep line to manage relationships among segments for faster testing. Explore how to handle degenerate cases and ordering segments. Consider an event-point schedule for optimal performance.
E N D
n Line Segments ProblemGiven a set of nline segments in the plane,determine whether or not any intersection exists. It is not required to output all the intersections.
2 Running time (n ). 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. Nevertheless, sparsedistribution in practice: Most segments do not intersect, or if they do, only with a few other segments. Need an faster algorithm for testing in 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
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 segments by 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 Atotal orderover the segments that intersect the current position of the sweep line: b b > c > d (a and e are out of the ordering) a e c d Such a total order T is called thesweep-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 replaced by cross-product comparisons). O(lg n)for each operation.
Sweeping around an Intersection The order of the two intersecting segments gets reversed. b a e c f d beforeafter d < f < c < b d < c < f < b
Event-point Schedule Where will the sweep line make intermediate stops? A sequence of x-coordinates, in increasing order. The sweeping algorithm does something only when it reaches an endpoint. Sort the segment endpoints by increasing x-coordinate and proceed from left to right.
An Example e d a c f b d a c b e d c b a a b a c b d c b e d b Sweep-line status: Intersect!
Above(T, s) p s Below(T, s) Above(T, s) s p Below(T, s) 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 ifp is the left endpoint of a segment s then Insert(T, s) if (Above(T, s) exists and intersectss) or (Below(T, s) exists and intersects s) then returntrue ifp is the right endpoint of a segment s then if both Above(T, s) and Below(T, s) exist and they intersect then returntrue Delete (T, s) return false
Sorting of line segments takesO(n lg n) time. There are 2n event points, each costs O(lg n) on updating the sweep-line status. Running Time Total timeO(n lg n).