Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

21/11/2020 UOR_7.2.

7.2.4 Do n Line Segments Intersect?

Suppose now that we are given n straight-line segments on a plane with each
segment specified by its two end points. An interesting question then is: Do any of
these line segments intersect? The straightforward approach requires 0(n2) time:
there are n(n - 1)/2 possible pairs of line segments which must be checked for
intersections, and checking for the intersection of any given pair of line segments
requires constant time. Once again, however, the most straightforward approach is
not the most efficient one. We describe next an algorithm that requires 0(n log2 n)
time [SHAM 76] to answer the problem: "Is there at least one intersection? If so,
identify one." In the x-y coordinate system we shall call the left (right) end point of
a given line segment that end point of the segment with the smaller (larger) value
of the x-coordinate.6 Suppose now that we begin "sweeping" a vertical line along
the x-axis (Figure 7.17). Two line segments S and T are then said to be comparable
at x = x1 if they are both intersected by the vertical line at x = x1 The segments are
consecutive if S is immediately above or immediately below T; in the former case
we write S = above (T, x1), in the latter S = below (T, x1). Note that it is possible
that no line segment will be above and/or below a line segment T for some or all
values of x.

The key observation leading to the algorithm below is that if two line segments S
and T intersect for some value of x = x1, they must be consecutive7 for some range
of x ‹ x1. The algorithm thus concentrates on those line segments which become
consecutive for some range of values of x.

The algorithm begins by sorting the 2n end points from left to right according to
the values of their x-coordinates and then sweeps through the 2n end points from
left to right. During the sweep, the algorithm maintains a list of current line
segments which is sorted according to the order in which the line segments
intersect the vertical sweep line. This sorted list is updated every time an end point
is encountered: if the end point is a left end point, the corresponding line segment
is added to the sorted list at the appropriate place; if it is a right end point, it is
deleted from the list (see also Figure 7.18). The algorithm performs a check on
https://web.mit.edu/urban_or_book/www/book/chapter7/7.2.4.html 1/3

You might also like