You are on page 1of 1

21/11/2020 UOR_7.2.

whether two line segments intersect only when they become consecutive in the
sorted list of line segments. It stops and returns the names of two intersecting line
segments as soon as it finds an intersection.

We can now present the details of the algorithm:

Algorithm for the Intersection of Line Segments (Algorithm 7.1)


STEP Sort the x-coordinates of the 2n end points of the line segments from left to
1: right. Begin processing the 2n end points at the leftmost end point.
STEP Let xi denote the x-coordinate of the end point currently being processed
2: and X the line segment corresponding to that end point.

1. If this is the left end point of X: a. Add X to the sorted list of current line
segments, at its appropriate place according to the y-coordinate of the end
point. b. If Y = above (X, xi), check to determine if Y and X intersect. If they
do, stop and return " Y and X." If they do not, continue. c. If Z = below (X,
xi), check to determine if Z and X intersect. If they do, stop and return "Z
and X." If they do not, go to Step 3. 2. If this is the right end point of X: If Y
= above (X, xi) and Z = below (X, xi), check whether Y and Z intersect. If
they do, stop and return " Y and Z." If they do not, delete X from the list of
current line segments.
STEP If the last end point processed was the rightmost end point, stop and return
3: "no intersection." If not, go to the next end point (to the right) and return to
Step 2.

To prove that this algorithm works, (i.e., that it finds an intersection if there is at
least one), it is sufficient to show (see Problem 7.5) that if the algorithm is not
stopped when it finds its first intersection, it is guaranteed to find eventually the
leftmost intersection of the line segments. This statement emphasizes the fact that
the first intersection discovered by Algorithm 7.1 is not necessarily the leftmost
intersection. This peculiarity is illustrated in Figure 7.19. Another peculiarity of
Algorithm 7.1 is that, if allowed to process all end points, it may "miss" some
intersections (see again Figure 7.19), although, as we just stated, it will never miss
the leftmost one.

https://web.mit.edu/urban_or_book/www/book/chapter7/7.2.4.html 2/3

You might also like