Segment
Intersection
Wei Qian
A. Introduction.
In this project,
I try to solve the problem defined as: given a set S of n closed segments
in the plane, report all intersection points among the segment in
S. This doesn't seem like a challenging problem: we can take each pair
of segments, compute whether they intersect, if so, report their intersection
point. This brute-force algorithm clear requires O(n2)
time. This is not a practical algorithm in real life. Here I use
plane sweep algorithm in stead, in which, there is an imagery line
swept across the plane. The status of the sweep line is the set
of segments intersecting it. The status changes while the sweep line moves
to the right, but not continuously. Only at particular points is an update
of the status required. These points are called event points of
the plane sweep algorithm. In this algorithm, the event points are the
endpoints of the segments and the intersection points.
B. Algorithm.
Here is the detail of the sweep line algorithm for segment intersection.
Algorithm FINDINTERSECTIONS(S)
Input. A set of line segments in the plane.
Output. The set of intersection points among the segments in
S.
-
Initialize an empty event queue Q. Next, insert the segment endpoints
into Q; each endpoint stores the segments begin from and/or end
at itself.
-
Initialize an empty status structure T.
-
While Q is not empty
-
do Determine the next event point p in Q and delete
it.
-
HANDLEENENTPOINT(p).
HANDLEENENTPOINT(p)
-
Let L(p) be the set of segments whose left endpoint is p,
and R(p) the segments whose right endpoint is p. For vertical
segment, the upper endpoint is defined as the left one.
-
Search in T for the set C(p) of all segments that contain
p in their interior.
-
if L(p)UR(p)UC(p) contains more than one segments
-
then Report p an an intersection point.
-
Delete the segments in R(p)UC(p) from T.
-
Insert the segments in L(p)UC(p) into T. The order
of the segments in T should correspond the the order in which
they are intersected by a sweep line just right to p. If there is
a vertical segment, it comes last among all segments containing p.
-
if L(p)UC(p) = 0
-
then Let su and sl
be the upper and lower neighbors of p in T.
-
FINDNEWEVNET(su, sl ,
p).
-
else Let s' be the uppermost segment
of L(p)UC(p) in T.
-
Let su be the upper neighbor of s' in T.
-
FINDNEWEVNET(sl, s', p).
-
Let s" be the lowest segment of L(p)UC(p) in T.
-
Let sl be the lower neighbor of s" in T.
-
FINDNEWEVNET(sl , s",
p).
FINDNEWEVNET(su,
sl , p)
-
if su and sl intersect
to the right of the sweep line, or on it and below the current event
point, and the intersection is not yet present as an event in Q
-
then Insert the intersection point as an
event into Q.
The running time of Algorithm FINDINTERSECTIONS for a set
of S of n line segments in the plane is O(nlogn
+ Ilogn), where I is the number of intersection
points of segments in S.
C. Animation
If your netscape/explorer has problems with Java JDK 1.1, go here.
Reference:
1. M. de Berg, M. van Kreveld, M. Overmars and
O. Schwarzkopf, Computational
Geometry Algorithms and Applications. Springer-Verlag, March
1997.
2. Core JAVA, 1998
Wei Qian
Last
modified: Thu Sep 3 23:13:00 EDT 1998