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 to distinguish four inputs. All are closed segments and include both endpoints.
| Input | Left endpoint | Right endpoint | Containing line and horizontal range |
|---|---|---|---|
| , | |||
| , | |||
| , | |||
| , |
You can check the equations using the endpoints. For , when , ; when , . For , when , ; when , . 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.
Each grid interval represents one unit on both axes, and all figures use the same scale. 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 . 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 , the neighbors are and , and and ; and 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 coordinate | Known event |
|---|---|
| starts | |
| starts | |
| starts | |
| ends | |
| starts | |
| ends | |
| ends | |
| 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 , starts. The active order consists only of , with no neighbor to check.
At , starts. The height of is and that of is , giving order . Check the pair: one requires and the other , which cannot both hold. They have no intersection.
At , starts. Its height is , above at , so the order becomes .
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 and . A common point's vertical coordinate must satisfy both and , so
Add to both sides to obtain , then subtract from both sides to obtain . Substituting into , the two lines intersect at .
However, ends at . Since , this point is not on , so no future crossing is added to the list. We still do not check against , because 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 , ends. Removing it changes the order from to .
Now and are neighbors. The height of a common point satisfies
Add to both sides to obtain , then subtract to obtain . Substitute into : the intersection is , which we call .
Check the bounds: is within 's range from to and 's range from to . It is indeed an intersection of the segments. Since , it lies to the right of the current sweep position, so add to the list.
A hollow circle marks an intersection already discovered but not yet reached. The remaining horizontal coordinates in the list are . Discovering does not mean jumping straight to it: we must process the endpoint at 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 , starts at . Here has height and has height , so the order after processing is .
The only new adjacency is with ; there is no segment below . Set their heights equal:
Add to both sides to obtain . Substitute into : the intersection is , which we call . The value is within both 's range from to and 's range from to . Also, , so we can add it to the list.
The remaining positions are now . Although was discovered earlier, must be processed first because . 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 , the sweep line reaches . Report “input pair and , with intersection point .”
Why does their order change? The height of is always . For , when , , so it is below ; when , , so it is above . Thus the order before processing, , becomes afterward.
and are still neighbors, but their vertical order is reversed. The new adjacency is with . Check this pair:
Add to both sides to obtain . Add to both sides to obtain . Divide both sides by the nonzero number to obtain
Substitute into :
Call this intersection . Its horizontal coordinate is within 's range from to and 's range from to . Since , it lies to the right of the current position, so add it to the list.
A filled intersection point has already been reported. The remaining positions in the list are now . and are temporarily not neighbors, but their registered real intersection 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 and cross, it becomes . Check and , 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 , report “input pair and , with intersection point .”
The height of minus the height of is
When , multiply both sides by positive to get , so and is below . When , the same operation gives , so and is above . The order therefore changes from to .
The new adjacency is with . The check again gives , but it is already in the list, so we do not add a second copy. The remaining positions are .
At P, A and C Exchange Order
At , report “input pair and , with intersection point .” Their height difference, minus , is : positive to the left of , negative to its right. Thus the order changes from to .
The new adjacency is with . Their intersection is at , 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 ending at , ending at , and ending at , 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 event | Order afterward | Neighbors checked now | Intersection newly added to list | Reported now |
|---|---|---|---|---|
| , starts | None | None | None | |
| , starts | : no intersection | None | None | |
| , starts | : line intersection beyond | None | None | |
| , ends | None | |||
| , starts | None | |||
| , reaching | at | |||
| , reaching | : already listed | None | at | |
| , reaching | : already to the left | None | at | |
| , ends | None | None | None | |
| , ends | None | None | None | |
| , ends | Empty | None | None | None |
The table shows two sources of new neighbors that are easy to overlook: at , the intervening ends, bringing together ; at , cross, bringing together . 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 discovered before but reported after it? Why do removing and exchanging 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.