Skip to main content

What Are We Trying to Compute?

If someone gives you several line segments and asks where they intersect, what would you do? A direct approach is to take two segments, check whether they share any part, and then move on to another pair until every pair of distinct inputs has been checked.

This approach is easy to think of. Before looking for a method that does less work, however, we need to clarify two things: what do we check each time, and what result should we keep?

This is Chapter 1 of the Bentley–Ottmann segment intersection series. We first establish the problem and a baseline method. The next chapter studies how a sweep line can reduce the number of segment pairs that need attention.

Each Input Has Its Own Identity​

A segment can be described by two endpoints. For example, the segment with endpoints (0,0)(0,0) and (4,0)(4,0) is the part of the horizontal axis from 00 to 44.

The segments we discuss include both endpoints. Such a segment is called a “closed segment.” Thus, both (0,0)(0,0) and (4,0)(4,0) lie on this segment. A line extends indefinitely in both directions, but a segment has a limited extent: (5,0)(5,0) lies on the line extending this segment, but not on the segment itself. To review how an equation and coordinate bounds work together to describe a segment, see Writing a Line Equation from Two Endpoints.

The two endpoints may also be the same. If both are (2,0)(2,0), the input contains only the point (2,0)(2,0) and has length 00. We accept inputs like this too.

Now number the inputs: input 11, input 22, input 33, and so on. A number distinguishes one input from another; it does not describe the segment's length or location.

Suppose we have the following three inputs. The figures label them S1\teachBlue{S_1}, S2\teachOrange{S_2}, and S3\teachViolet{S_3}; the subscript is the input number:

Input numberEndpoints
11(0,0)(0,0) and (4,0)(4,0)
22(0,0)(0,0) and (4,0)(4,0)
33(4,0)(4,0) and (0,0)(0,0)

They occupy exactly the same geometric location. The third input merely lists its endpoints in reverse order, which does not change the points it contains. Nevertheless, these are three inputs. We must not silently merge them into one just because their drawings coincide.

Input 1: the closed segment from (0,0) to (4,0).Input 1: the closed segment from (0,0) to (4,0).
Input 2: the closed segment from (0,0) to (4,0), coinciding with input 1.Input 2: the closed segment from (0,0) to (4,0), coinciding with input 1.
Input 3: endpoints listed as (4,0), (0,0); its geometric location is the same as the first two inputs.Input 3: endpoints listed as (4,0), (0,0); its geometric location is the same as the first two inputs.

The three panels show inputs 11, 22, and 33 in order. When space is limited, they wrap onto another row; read from left to right and then top to bottom. Every panel retains its original size and uses the same coordinate range and scale. Coincident segments have not been shifted apart. Although the third input lists its endpoints in reverse order, its segment is unchanged. The arrow below each panel shows only the order in which the endpoints are written; it does not give the segment a direction. In the figures, OO denotes the origin, axis arrows indicate positive directions, and filled endpoint circles show that the endpoints are included.

We use “input pair 11 and 22” to mean the two inputs numbered 11 and 22. This is the same pair as “input pair 22 and 11,” because reversing the order of checking does not change their common part. This is called an “unordered input pair.” We choose two distinct inputs and never check an input against itself.

What Can Two Segments Have in Common?​

All points belonging to both segments together form their “intersection.” If they have no common point, their intersection is empty.

Fix a segment A\teachBlue{A} from (0,0)(0,0) to (4,0)(4,0). Every point on it satisfies y=0y=0, and its horizontal coordinate ranges from 00 to 44, including both ends. In each example below, choose one other segment and compare it with AA alone.

In the figures, B\teachOrange{B} labels the other input, and A∩B\teachViolet{A\cap B} denotes the intersection of the two inputs. The symbol ∩\cap is read as “intersection.” When there is no common point, we denote the empty intersection by ∅\varnothing.

No Common Point: An Empty Intersection​

Let the other segment run from (0,1)(0,1) to (4,1)(4,1). All its points satisfy y=1y=1, whereas all points on AA satisfy y=0y=0. A point's vertical coordinate cannot be both 00 and 11, so the segments have no common point.

We have checked this input pair, but it produces no intersection result record.

Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: from (0,1) to (4,1).Input B: from (0,1) to (4,1).
A and B have no common point; their intersection is empty.A and B have no common point; their intersection is empty.

Here and in the groups below, the three panels are ordered as “input A, other input B, complete intersection.” Continue in that order if the panels wrap. All use the same coordinate range and scale. Coordinates are shown beside endpoints. The third panel shows only the intersection, without drawing the inputs again; an empty intersection is marked ∅\varnothing. Thin gray lines are coordinate axes, not parts of the inputs or results.

Exactly One Common Point: A Point Intersection​

Replace the other segment with the vertical segment from (2,−1)(2,-1) to (2,1)(2,1). Every point on it satisfies x=2x=2. A common point must also lie on AA, so its vertical coordinate must be 00.

These two conditions leave only (2,0)(2,0). Its horizontal coordinate 22 is between 00 and 44, and its vertical coordinate 00 is between −1-1 and 11, so it really lies on both segments. Their intersection is this one point.

Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: from (2,-1) to (2,1).Input B: from (2,-1) to (2,1).
The complete intersection of A and B is the point (2,0).The complete intersection of A and B is the point (2,0).

Touching at an endpoint also counts as intersecting. For example, replace the other segment with the horizontal segment from (4,0)(4,0) to (6,0)(6,0). A common point's horizontal coordinate can be neither greater than 44 nor less than 44, so it must equal 44. Both segments include this endpoint, making their intersection (4,0)(4,0).

Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: from (4,0) to (6,0).Input B: from (4,0) to (6,0).
The complete intersection of A and B is their shared endpoint (4,0).The complete intersection of A and B is their shared endpoint (4,0).

Zero-length inputs follow the same definition. If the other input contains only (2,0)(2,0), that point lies on AA and is the intersection. If it contains only (5,0)(5,0), the point is outside the bounds of AA, so the intersection is empty. Two point inputs have a common point only when their locations are the same.

Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: the point (2,0), marked by a diamond, lies on A.Input B: the point (2,0), marked by a diamond, lies on A.
The complete intersection of A and B is the point (2,0), marked by a dot.The complete intersection of A and B is the point (2,0), marked by a dot.
Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: the point (5,0), outside the bounds of A.Input B: the point (5,0), outside the bounds of A.
The intersection of A and B is empty.The intersection of A and B is empty.

The diamond in each group's second panel identifies an input consisting of just one point. It does not mean the input has area. The third panel still uses a dot for a point intersection.

A Shared Segment: An Overlap Interval​

Now let segment BB run from (2,0)(2,0) to (6,0)(6,0). Both it and AA lie on the horizontal axis.

A common point must satisfy both

0≤x≤4,2≤x≤6.\teachBlue{0}\le x\le\teachBlue{4},\qquad \teachOrange{2}\le x\le\teachOrange{6}.

The second condition requires xx to be at least 22, and the first requires it to be at most 44. Thus a common point must satisfy 2≤x≤42\le x\le4.

Conversely, take any number satisfying 2≤x≤42\le x\le4. It is between 00 and 44 and also between 22 and 66. Therefore, (x,0)(x,0) belongs to both segments.

Together, these two directions show that the intersection is exactly the complete closed interval from (2,0)(2,0) to (4,0)(4,0), including both endpoints. Its length is 4−2=24-2=2; it is not just a single point.

Reporting only two endpoint intersections would be wrong, because intermediate points such as (3,0)(3,0) also belong to the intersection. We use the interval's two endpoints to describe the entire common part, not to claim that only those two points are shared.

Input A: from (0,0) to (4,0).Input A: from (0,0) to (4,0).
Input B: from (2,0) to (6,0).Input B: from (2,0) to (6,0).
Complete intersection: from (2,0) to (4,0), including both endpoints and every point between them.Complete intersection: from (2,0) to (4,0), including both endpoints and every point between them.

If we add a segment CC from (3,−1)(3,-1) to (3,1)(3,1), it intersects AA and BB separately at (3,0)(3,0). These are results for two other input pairs. The points contained in AA and BB have not changed, so their intersection remains one complete interval from (2,0)(2,0) to (4,0)(4,0). It must not be split into two results at (3,0)(3,0).

Before adding C
Before adding C: input A runs from (0,0) to (4,0).Before adding C: input A runs from (0,0) to (4,0).
After adding C
After adding C: A is unchanged; C runs from (3,-1) to (3,1).After adding C: A is unchanged; C runs from (3,-1) to (3,1).
Before adding C
Before adding C: input B runs from (2,0) to (6,0).Before adding C: input B runs from (2,0) to (6,0).
After adding C
After adding C: B is unchanged; C runs from (3,-1) to (3,1).After adding C: B is unchanged; C runs from (3,-1) to (3,1).
Before adding C
Before adding C: the complete intersection of A and B runs from (2,0) to (4,0).Before adding C: the complete intersection of A and B runs from (2,0) to (4,0).
After adding C
After adding C: the complete intersection of A and B still runs from (2,0) to (4,0), without being split.After adding C: the complete intersection of A and B still runs from (2,0) to (4,0), without being split.

These six panels form three before-and-after pairs. Read each pair as “before adding C” and then “after adding C.” If there is not enough space, the second panel wraps onto the next row but remains in the same group. The first two pairs show A and B separately alongside the newly added C, so that coincident segments do not hide one another. The last pair shows only the intersection of A and B, which remains the same complete interval. This compares the intersections before and after adding an input; it is not a sweep-line execution sequence.

Why Can the Common Part Not Be Two Separate Pieces?​

We have seen empty intersections, points, and intervals of nonzero length. For closed segments, these cover every possibility.

First consider an input of length 00. It contains only one point, so the common part can only be that point or the empty set; it cannot contain more points.

Now consider two segments of nonzero length. If the common part has no points, it is empty; if it has exactly one, it is a point intersection. What remains to show is this: as soon as there are two distinct common points, the common part must be one complete segment.

Call the two distinct common points PP and QQ. Two distinct points determine one line. The lines containing both segments pass through PP and QQ, so they are the same line.

If this line is not vertical, order its points by increasing horizontal coordinate. On this line, each horizontal coordinate corresponds to exactly one point. A segment extends continuously from one endpoint to the other, containing the points at every horizontal coordinate in between. If the line is vertical, use vertical coordinates instead; the reasoning is the same.

Compare the segments in this order. Let LL be the later of their two earlier endpoints, and let RR be the earlier of their two later endpoints.

No common point can precede LL: it would be before the beginning of one segment. Nor can it follow RR: it would be beyond the end of one segment. Thus every common point must lie between LL and RR.

Conversely, every point from LL to RR is no earlier than either segment's earlier endpoint and no later than either segment's later endpoint. It therefore belongs to both segments. Since we already have two distinct common points, LL must precede RR and cannot coincide with it.

The common part is therefore exactly the complete closed interval of nonzero length from LL to RR; there can be no gap inside it. This establishes all three possibilities. Here we use this fact to specify the shape of a result. Chapter 3 will explain how to compute the result from arbitrary endpoints.

Record Both the Location and the Input Pair​

Consider this small example with three inputs:

Input numberEndpoints
11(−1,0)(-1,0) and (1,0)(1,0)
22(0,−1)(0,-1) and (0,1)(0,1)
33(0,0)(0,0) and (0,0)(0,0)

Input 11 lies on the horizontal axis, and input 22 on the vertical axis. Their only common point is the origin (0,0)(0,0): a common point must satisfy both x=0x=0 and y=0y=0, and the origin is within both segments' endpoint bounds. Input 33 contains only the origin, which also belongs to each of the first two segments. The results are therefore:

Input pairComplete intersection
11 and 22Point (0,0)(0,0)
11 and 33Point (0,0)(0,0)
22 and 33Point (0,0)(0,0)
Input 1: the horizontal segment from (-1,0) to (1,0).Input 1: the horizontal segment from (-1,0) to (1,0).
Input 2: the vertical segment from (0,-1) to (0,1).Input 2: the vertical segment from (0,-1) to (0,1).
Input 3: only the origin, marked by a diamond.Input 3: only the origin, marked by a diamond.
All three inputs in the same coordinate system; each contains the origin (0,0).All three inputs in the same coordinate system; each contains the origin (0,0).

Reading left to right and then top to bottom, the first three panels show inputs 11, 22, and 33 separately, and the fourth places them together. All three contain the origin, but the results must still list the three input pairs separately, as in the table. The diamond still denotes the point input at the origin; we must not omit this input merely because it coincides with the intersection of the two segments.

There is only one geometric location but three intersecting input pairs, so we keep three results. Writing only “the origin” would not tell us which inputs intersect one another.

Return to the three coincident inputs at the start. They also form three pairs: 11 and 22, 11 and 33, and 22 and 33. Each input contains exactly the same points, so every pair's intersection is the complete interval from (0,0)(0,0) to (4,0)(4,0). Coinciding geometrically does not remove any input's identity.

We can therefore state our goal precisely: find every unordered pair of original inputs with a nonempty intersection, and keep one result for each pair, recording the input pair and its complete intersection.

How Do We Check Every Pair Once?​

Practice enumerating pairs with four inputs. Here we list only the pairs to check; their locations do not affect this enumeration.

Current inputPair only with higher-numbered inputsChecks in this row
1111 and 22, 11 and 33, 11 and 4433
2222 and 33, 22 and 4422
3333 and 4411
44No higher-numbered input00

This checks 3+2+1=63+2+1=6 pairs in total. For each pair, compute the intersection of its two inputs. Skip empty intersections, and record nonempty ones using the rules above.

The same method works for more inputs. Let us explain why it misses no pair and checks none twice.

Why is no pair missed? Take any two distinct inputs. One must have the smaller number and the other the larger. In the row for the smaller number, the rule pairs it with every larger number, so this pair must be checked. This applies to any two distinct inputs, regardless of their positions or whether they intersect.

Why is no pair repeated? A pair can occur only in the row for its smaller number. In the row for the larger number, the rule allows only still larger numbers; it never returns to the smaller one. In the smaller number's row, each larger number occurs only once. Thus the pair is checked exactly once in total.

The rule also avoids pairing an input with itself, because “higher-numbered” excludes an equal number. Provided each two-segment check computes the complete intersection correctly, the enumeration finds every intersecting input pair and keeps at most one result per pair. This is the baseline against which we can compare later algorithms.

How Many Pairs Must Be Checked?​

Let nn denote the number of original inputs. For example, with four inputs, n=4n=4. We are counting inputs, so coincident inputs still count separately.

First suppose n≥2n\ge2. To count the distinct pairs, temporarily allow both possible orders. There are nn choices for the first input and then n−1n-1 choices for a different second input. Each first choice has n−1n-1 second choices, giving a total of

n(n−1).n(n-1).

But this counts each pair twice: for example, choosing 11 then 22, and choosing 22 then 11. Every pair of distinct inputs has exactly these two orders and no third; we need only one. Dividing the count by 22 gives

n(n−1)2.\frac{n(n-1)}{2}.

We have already proved that the pairwise method checks each unordered input pair exactly once, so this is also its number of checks.

Substitute n=4n=4, doing the subtraction in parentheses before multiplication and division:

4(4−1)2=4×32=122=6,\frac{4(4-1)}{2} =\frac{4\times3}{2} =\frac{12}{2} =6,

which agrees with the 66 pairs counted row by row in the table.

With no inputs or only one, we cannot choose two distinct inputs, so there are 00 checks. The formula also gives 0×(0−1)/2=00\times(0-1)/2=0 and 1×(1−1)/2=01\times(1-1)/2=0. We establish these boundary cases first from “there is no pair to choose”; there is no need to interpret a negative number as a number of choices.

The number of checks grows quickly as the input count increases:

Input count nnNumber of checks
101010×9/2=4510\times9/2=45
100100100×99/2=4950100\times99/2=4950
100010001000×999/2=4995001000\times999/2=499500

Even if no two segments intersect, the pairwise method still checks this many pairs and produces no intersection results. Thus the number of pairs checked and the number of pairs that actually intersect are different quantities.

On the other hand, inputs for which every pair intersects do exist. For example, let all nn inputs occupy the same nonzero-length segment. Every pair's intersection is that entire segment, so the number of result records is also n(n−1)/2n(n-1)/2. If the results must be listed one by one, no method can avoid the work of outputting them.

Also, the number of checks is not the complete running time. Each check involves geometric decisions and calculations, and storing a result takes work too. With exact fractions, arithmetic can also become more expensive as the numbers get longer. This chapter counts only “how many pairs must be checked”; it does not claim that a check takes a fixed amount of real time. Later chapters will develop the full workload analysis.

From the Baseline to a Sweep Line​

We now have a precise goal: organize results by original input pair, retaining a point or one complete overlap interval for each intersecting pair. We also have a reliable baseline: checking pairs one by one misses none and repeats none, but checks n(n−1)/2n(n-1)/2 pairs regardless of how many actually intersect.

The next question is: when many segments do not intersect, can we use their relative positions to check fewer pairs while still finding every result?

Chapter 2, in Part 1: The Sweep and Three Types of Events and Part 2: Why No Intersection Is Missed, explores this question under explicit simplifying assumptions.