Skip to main content

The Sweep and Three Types of Events

The previous chapter, What Are We Trying to Compute?, established pairwise checking as a baseline. Checking every input pair for a common part misses no result, but many of those segments are far apart.

This time, we move a vertical auxiliary line from left to right and stop only where necessary. This is called a “sweep line.” Our question is: if we check only segments that are immediately next to each other vertically, might we miss a pair that starts far apart and intersects later?

Chapter 2 has two parts. Here we follow an example to see how neighbors change during the sweep and how future intersections are discovered. Part 2: Why No Intersection Is Missed then extends the reasoning to every input allowed in this chapter. After this part, you can pause and try to explain the three event rules in your own words.

State the Assumptions First​

To see the basic mechanism clearly, we temporarily require the following:

  • Every segment has positive length and is nonvertical.
  • No two segments overlap, no endpoint lies on another segment, and no point lies on three or more segments.
  • All endpoints and intersections have different horizontal coordinates.

Thus, each intersection lies in the interiors of exactly two segments. At each stop, we handle one segment starting, one segment ending, or two segments crossing. Horizontal segments and nonintersecting segments are allowed.

These are teaching assumptions for this chapter, not reasons for the complete algorithm to reject more complicated inputs. Later chapters will remove them one by one. Results are still recorded by original unordered input pair; under these assumptions, every nonempty intersection happens to be a point.

Four Segments, Without Giving Away the Intersections​

Use A,B,C,D\teachBlue{A},\teachOrange{B},\teachViolet{C},\teachEmerald{D} to distinguish four inputs. All are closed segments and include both endpoints.

InputLeft endpointRight endpointContaining line and horizontal range
A\teachBlue{A}(0,1)(0,1)(10,1)(10,1)y=1\teachBlue{y}=\teachBlue{1}, 0≤x≤100\le x\le10
B\teachOrange{B}(1,3)(1,3)(3,3)(3,3)y=3\teachOrange{y}=\teachOrange{3}, 1≤x≤31\le x\le3
C\teachViolet{C}(2,6)(2,6)(9,−1)(9,-1)y=8−x\teachViolet{y}=\teachViolet{8-x}, 2≤x≤92\le x\le9
D\teachEmerald{D}(4,0)(4,0)(8,4)(8,4)y=x−4\teachEmerald{y}=\teachEmerald{x-4}, 4≤x≤84\le x\le8

You can check the equations using the endpoints. For CC, when x=2x=2, y=8−2=6y=8-2=6; when x=9x=9, y=8−9=−1y=8-9=-1. For DD, when x=4x=4, y=4−4=0y=4-4=0; when x=8x=8, y=8−4=4y=8-4=4. Two endpoints determine the containing line, and the bounds restrict it to the segment. To review the distinction, see Writing a Line Equation from Two Endpoints.

Four inputs: A from (0,1) to (10,1), B from (1,3) to (3,3), C from (2,6) to (9,-1), and D from (4,0) to (8,4). Intersections have not yet been marked.Four inputs: A from (0,1) to (10,1), B from (1,3) to (3,3), C from (2,6) to (9,-1), and D from (4,0) to (8,4). Intersections have not yet been marked.

Each grid interval represents one unit on both axes, and all figures use the same scale. OO denotes the origin. The figure lets us see the complete inputs, but no input pair has been checked yet. An intersection we can spot by eye is not yet a computed result.

The figures in this part are teaching diagrams arranged to follow the explanation. Each keeps a fixed size. They contain only short mathematical labels; detailed steps appear in the prose and record table.

What Do We Keep Track of During the Sweep?​

When a segment starts, ends, or crosses another, the segments met by the sweep line or their vertical relationship can change. A position where we must stop and process such a change is called a “sweep event.” The two records below answer “who is next to whom now?” and “where do we go next?”

An Order from Bottom to Top​

The sweep line's position is given by its horizontal coordinate xx. Away from endpoints and intersections, input segments meeting the sweep line are called the current “active segments.” Since no input is vertical, each active segment meets the sweep line at one point. We can arrange the segments from bottom to top by those points' heights. This list is called the “active order.”

“Neighbors” means two immediately consecutive segments in this list, with no active segment between them. For example, in the order A,B,CA,B,C, the neighbors are AA and BB, and BB and CC; AA and CC are not neighbors yet.

At an event, distinguish before processing from after processing. Before processing, use the order immediately to the left; afterward, use the order immediately to the right. “Immediately” means without crossing another event, not moving an arbitrary fixed distance. At an intersection the two heights are equal, but the orders on its left and right are each well defined.

A List of Pending Events​

All endpoints are known from the start, so place them in a “pending list,” sorted by increasing horizontal coordinate. Each time, take the leftmost position from this list. Future intersections newly discovered during processing are also inserted in horizontal-coordinate order.

The initial list for our example is:

Horizontal coordinateKnown event
00AA starts
11BB starts
22CC starts
33BB ends
44DD starts
88DD ends
99CC ends
1010AA ends

This list does not contain any crossing events yet. “Discovering” means checking a segment pair and computing and confirming its intersection. “Registering” means placing an intersection we have not reached into the list for later processing. “Reporting” happens when the sweep line actually reaches the intersection: only then do we record this input pair's result and handle its order change. These are not all the same moment.

Register a given crossing only once. In this chapter, we retain every real future intersection already discovered: even if its two segments temporarily stop being neighbors, we do not remove it. If another check finds the same intersection, we do not insert a duplicate. This keeps the explanation focused on when intersections are discovered, without introducing a procedure for canceling scheduled events.

Learn About Neighbors from Start Events​

At x=0x=0, AA starts. The active order consists only of AA, with no neighbor to check.

At x=1x=1, BB starts. The height of AA is 11 and that of BB is 33, giving order A,BA,B. Check the pair: one requires y=1y=1 and the other y=3y=3, which cannot both hold. They have no intersection.

At x=2x=2, CC starts. Its height is 8−2=68-2=6, above BB at 33, so the order becomes A,B,CA,B,C.

After processing C starting at x=2, the bottom-to-top active order is A, B, C. D has not started and is shown faintly. No intersections have been discovered.After processing C starting at x=2, the bottom-to-top active order is A, B, C. D has not started and is shown faintly. No intersections have been discovered.

The vertical dashed line marks the current event position. The bottom of the figure lists the bottom-to-top order after processing. Segments that have not started or have already ended are faint; their positions remain visible to help compare successive states.

The new neighbors are BB and CC. A common point's vertical coordinate must satisfy both y=3y=3 and y=8−xy=8-x, so

3=8−x.3=8-x.

Add xx to both sides to obtain 3+x=83+x=8, then subtract 33 from both sides to obtain x=5x=5. Substituting into y=3y=3, the two lines intersect at (5,3)(5,3).

However, BB ends at x=3x=3. Since 5>35>3, this point is not on BB, so no future crossing is added to the list. We still do not check AA against CC, because BB is between them.

Start-event rule: insert the new segment in the correct position and check it against its neighbors above and below. If one side has no neighbor, skip that side.

Why are these the only new checks needed? No other endpoint or intersection is at this start event, so the relative heights of the existing segments do not change. Inserting one segment can only split one old neighboring pair and create adjacencies on the new segment's two sides. Every other adjacency stays exactly as it was.

An Intervening Segment Ends, Revealing a Distant Intersection​

At x=3x=3, BB ends. Removing it changes the order from A,B,CA,B,C to A,CA,C.

Now AA and CC are neighbors. The height of a common point satisfies

1=8−x.\teachBlue{1}=\teachViolet{8-x}.

Add xx to both sides to obtain 1+x=81+x=8, then subtract 11 to obtain x=7x=7. Substitute into y=1y=1: the intersection is (7,1)(7,1), which we call PP.

Check the bounds: 77 is within AA's range from 00 to 1010 and CC's range from 22 to 99. It is indeed an intersection of the segments. Since 7>37>3, it lies to the right of the current sweep position, so add PP to the list.

After processing B ending at x=3, A and C are neighbors and the future intersection P=(7,1) is discovered. P is marked by a hollow circle and has not been reported.After processing B ending at x=3, A and C are neighbors and the future intersection P=(7,1) is discovered. P is marked by a hollow circle and has not been reported.

A hollow circle marks an intersection already discovered but not yet reached. The remaining horizontal coordinates in the list are 4,7,8,9,104,7,8,9,10. Discovering PP does not mean jumping straight to it: we must process the endpoint at x=4x=4 first.

End-event rule: remove the ending segment. If it had both a neighbor below and a neighbor above, check those two new neighbors. If there is a neighbor on only one side, removing the segment creates no new neighboring pair.

Again, the reason is a local change: no other segment crosses, starts, or ends here, so the others retain their relative order. Only the two segments formerly separated by the removed one can become adjacent.

A Newly Starting Segment Adds an Earlier Intersection​

At x=4x=4, DD starts at (4,0)(4,0). Here AA has height 11 and CC has height 8−4=48-4=4, so the order after processing is D,A,CD,A,C.

The only new adjacency is DD with AA; there is no segment below DD. Set their heights equal:

x−4=1.\teachEmerald{x-4}=\teachBlue{1}.

Add 44 to both sides to obtain x=5x=5. Substitute into y=1y=1: the intersection is (5,1)(5,1), which we call QQ. The value 55 is within both AA's range from 00 to 1010 and DD's range from 44 to 88. Also, 5>45>4, so we can add it to the list.

After processing D starting at x=4, the order is D, A, C. Q=(5,1) is discovered and precedes the previously discovered P in the list. Neither intersection has been reported.After processing D starting at x=4, the order is D, A, C. Q=(5,1) is discovered and precedes the previously discovered P in the list. Neither intersection has been reported.

The remaining positions are now 5,7,8,9,105,7,8,9,10. Although PP was discovered earlier, QQ must be processed first because 5<75<7. The list is sorted by position, not discovery time.

After a Crossing, New Neighbors Reveal a Third Intersection​

At Q, A and D Exchange Order​

At x=5x=5, the sweep line reaches QQ. Report “input pair AA and DD, with intersection point (5,1)(5,1).”

Why does their order change? The height of AA is always 11. For DD, when x<5x<5, x−4<1x-4<1, so it is below AA; when x>5x>5, x−4>1x-4>1, so it is above AA. Thus the order before processing, D,A,CD,A,C, becomes A,D,CA,D,C afterward.

AA and DD are still neighbors, but their vertical order is reversed. The new adjacency is DD with CC. Check this pair:

x−4=8−x.\teachEmerald{x-4}=\teachViolet{8-x}.

Add xx to both sides to obtain 2x−4=82x-4=8. Add 44 to both sides to obtain 2x=122x=12. Divide both sides by the nonzero number 22 to obtain

x=6.x=6.

Substitute into y=x−4y=x-4:

y=6−4=2.y=6-4=2.

Call this intersection R=(6,2)R=(6,2). Its horizontal coordinate 66 is within CC's range from 22 to 99 and DD's range from 44 to 88. Since 6>56>5, it lies to the right of the current position, so add it to the list.

After processing the crossing at Q with x=5, the order is A, D, C. Q has been reported and is a filled circle. New neighbors D and C reveal the future intersection R=(6,2). P and R remain hollow circles.After processing the crossing at Q with x=5, the order is A, D, C. Q has been reported and is a filled circle. New neighbors D and C reveal the future intersection R=(6,2). P and R remain hollow circles.

A filled intersection point has already been reported. The remaining positions in the list are now 6,7,8,9,106,7,8,9,10. AA and CC are temporarily not neighbors, but their registered real intersection PP remains in the list.

Crossing-event rule: exchange the two crossing segments' vertical positions and check the newly created outer adjacencies. For example, if the order is L,U,V,HL,U,V,H and U,VU,V cross, it becomes L,V,U,HL,V,U,H. Check L,VL,V and U,HU,H, skipping any outer neighbor that does not exist. These letters simply represent four consecutive segments.

Why not check more distant segments? No third segment passes through this intersection under our assumptions, so the local change involves only these two segments; outside segments keep their relative order. The crossing segments remain each other's neighbors. They have just intersected, and two distinct lines cannot have a second intersection, so we do not need to look for another crossing of this same pair either. The next part will prove that the segments must indeed be neighbors immediately before crossing.

At R, C and D Exchange Order​

At x=6x=6, report “input pair CC and DD, with intersection point (6,2)(6,2).”

The height of DD minus the height of CC is

(x−4)−(8−x)=x−4−8+x=2x−12.(x-4)-(8-x)=x-4-8+x=2x-12.

When x<6x<6, multiply both sides by positive 22 to get 2x<122x<12, so 2x−12<02x-12<0 and DD is below CC. When x>6x>6, the same operation gives 2x>122x>12, so 2x−12>02x-12>0 and DD is above CC. The order therefore changes from A,D,CA,D,C to A,C,DA,C,D.

After processing the crossing at R with x=6, the order is A, C, D. Q and R have been reported. A and C become neighbors again and give P, but P is already in the list and is not registered twice.After processing the crossing at R with x=6, the order is A, C, D. Q and R have been reported. A and C become neighbors again and give P, but P is already in the list and is not registered twice.

The new adjacency is AA with CC. The check again gives P=(7,1)P=(7,1), but it is already in the list, so we do not add a second copy. The remaining positions are 7,8,9,107,8,9,10.

At P, A and C Exchange Order​

At x=7x=7, report “input pair AA and CC, with intersection point (7,1)(7,1).” Their height difference, CC minus AA, is (8−x)−1=7−x(8-x)-1=7-x: positive to the left of 77, negative to its right. Thus the order changes from A,C,DA,C,D to C,A,DC,A,D.

After processing the crossing at P with x=7, the order is C, A, D. All three intersections have been reported. The new neighbors A and D intersect at Q, to the left of the sweep line, so Q is not added to the list.After processing the crossing at P with x=7, the order is C, A, D. All three intersections have been reported. The new neighbors A and D intersect at Q, to the left of the sweep line, so Q is not added to the list.

The new adjacency is AA with DD. Their intersection QQ is at x=5x=5, already to the left of the sweep line. It is not a future event, so it is not added to the list again.

Next process DD ending at x=8x=8, CC ending at x=9x=9, and AA ending at x=10x=10, in that order. In none of these cases does the ending segment have old neighbors on both sides, so no new neighboring pair is created. Finally, both the active order and the pending list are empty, and the sweep ends.

Put Every Event into One Record Table​

The order in this table is always from bottom to top, after processing. A “new check” means a pair checked now because an adjacency changed. The pair may have been checked before; this does not necessarily mean its first check.

Current eventOrder afterwardNeighbors checked nowIntersection newly added to listReported now
x=0x=0, AA startsAANoneNoneNone
x=1x=1, BB startsA,BA,BA,BA,B: no intersectionNoneNone
x=2x=2, CC startsA,B,CA,B,CB,CB,C: line intersection beyond BBNoneNone
x=3x=3, BB endsA,CA,CA,CA,CP=(7,1)P=(7,1)None
x=4x=4, DD startsD,A,CD,A,CD,AD,AQ=(5,1)Q=(5,1)None
x=5x=5, reaching QQA,D,CA,D,CD,CD,CR=(6,2)R=(6,2)A,DA,D at QQ
x=6x=6, reaching RRA,C,DA,C,DA,CA,C: PP already listedNoneC,DC,D at RR
x=7x=7, reaching PPC,A,DC,A,DA,DA,D: QQ already to the leftNoneA,CA,C at PP
x=8x=8, DD endsC,AC,ANoneNoneNone
x=9x=9, CC endsAANoneNoneNone
x=10x=10, AA endsEmptyNoneNoneNone

The table shows two sources of new neighbors that are easy to overlook: at x=3x=3, the intervening BB ends, bringing together A,CA,C; at x=5x=5, A,DA,D cross, bringing together D,CD,C. Neither pair was adjacent at the start.

Pause and Connect the Three Rules​

Throughout the sweep, we maintain the bottom-to-top active order and the pending list sorted by horizontal coordinate.

  • A segment starts: insert it in the correct position and check its neighbors above and below.
  • A segment ends: remove it and check the two segments it used to separate.
  • Two segments cross: report this input pair's result, exchange their order, and check the new outer adjacencies.

Every check must confirm that the intersection belongs to both segments and lies strictly to the right of the current sweep position. Only then is it registered. Keep just one copy of each real future intersection, even if its segments temporarily stop being neighbors, and report it when reached.

Now cover the table and try answering: why was PP discovered before QQ but reported after it? Why do removing BB and exchanging A,DA,D both create new neighbors? If you can explain these two points, you have understood this part's sweep process.

Following one example does not yet prove that no allowed input can be missed. The remaining question is whether a pair that begins far apart must become neighbors before crossing, early enough to be checked. Part 2: Why No Intersection Is Missed proves this step by step, starting from height differences.