Handle everything at the same point together
Chapter 2 handled starts, ends, and crossings of two segments separately. What happens when one segment ends exactly where another begins? Which action comes first? If other segments also pass through that position, is swapping only the pair that discovered the intersection enough?
This is Chapter 4 of the segment-intersection series. Starting with contact between just two inputs, we build one rule: collect every same-point participant first, report all intersecting input pairs at this position, then update the active order, and finally check new neighbors.
Section 1: Why a shared endpoint must be reported before removal
We retain these teaching assumptions: nonzero segments are nonvertical, no two inputs have a positive-length overlap, and distinct event positions still have distinct horizontal coordinates. Other restrictions are lifted: several endpoints and segments may meet at one exact position, and zero-length point inputs are allowed. Collinear segments sharing only an endpoint are also allowed.
These conditions define this chapter's teaching scope, not the full algorithm's input restrictions. Overlaps, distinct positions with the same horizontal coordinate, and vertical segments are incorporated into the sweep in later chapters.
First take only two closed segments and name their common endpoint :
| Input | Left endpoint | Right endpoint | Supporting line and range |
|---|---|---|---|
| , | |||
| , |
All figures here are teaching diagrams arranged around the explanation, not real execution traces. Each has an original size of 320 × 260 and wraps onto a new row if space is limited. Read left to right, then top to bottom. One grid square is one coordinate unit, and is the origin. The table and text specify the exact endpoints and ranges.
By Chapter 1's convention that a closed segment contains its endpoints, belongs to both . Then apply Chapter 3's unique-candidate and closed-range checks: their supporting lines intersect at , and belongs to both closed ranges, so this pair's complete intersection is .
The active order immediately left of contains only , and immediately right only . If “ ends” means immediately forgetting , before handling “ starts,” one input is missing when pairs are formed, and the endpoint contact of is missed.
The correct order is to retain 's identity for this event, include as a participant, and report intersection for input pair . Only then does leave and enter the subsequent active order. Not extending farther right does not mean failing to contain the current closed endpoint.
This reasoning does not depend on the two lines' directions in the figure. Any input ending here still contains this point. If other participants are present, pairing at the current position must be completed before discarding ending members.
Section 2: An endpoint inside another input needs participants beyond the list
Keep and pair it only with a new input for this example; omit for now. runs from to on , with range . Substituting gives , and , so lies strictly inside .
's start is on the pending list from the beginning, but merely reading “ starts” has not located . Pairing only the endpoint inputs named in the list would again miss a result.
At the event's horizontal coordinate, reuse Chapter 3's exact height comparison at one position. Compute the heights of old active segments still covering . has height , equal to 's vertical coordinate, so it also passes through and must participate alongside starting input . The intersection result is point for pair .
The new rule is: after reading an endpoint, also find old active members at the same height. A segment is not irrelevant merely because none of its endpoints appears in the current list entry. Conversely, if only its extended line passes through while its segment range does not cover it, it is not a participant. Chapter 3's point-membership check must still pass.
Section 3: When many segments meet, the scheduled pair is not everyone
Now take and add . Observe only their event at , without replaying the entire sweep. runs from to on , with range . At , , and , so also passes strictly through .
All four inputs are now fixed; later examples retain these letters, coordinates, and meanings. Their other endpoints have horizontal coordinates , distinct from 's coordinate and from each other.
For example, checking old neighbors may already have discovered . That discovery establishes a future stop at , but does not imply that only should be processed on arrival. 's start, 's end, and the passages of all belong to the same event.
Register the position once and merge endpoint information
Reuse Chapter 3's result that both coordinates must respectively agree for points to be identical. If another input pair later computes too, use the same existing position in the list. Merge information about which inputs start and end there, rather than overwriting existing information.
Every input endpoint is known beforehand, so the list can retain the starters and enders at each position; point-input handling is added shortly. Even when a neighboring pair alone discovers an intersection, registration need only ensure that the real position stays on the list. Collect every participant anew on arrival; the pair that scheduled the event is not the full participant set.
Continue to use Chapter 2's rule of retaining real future intersections until removal from the list. Temporarily losing adjacency does not cancel a confirmed event position. The list returns the leftmost unprocessed position; each position is removed and handled together only once.
Use roles to distinguish current pairing from later membership
First assign roles to the current event's nonzero participants. Continue to write its position as , where are its horizontal and vertical coordinates, both in this example. The rules below do not depend on that value. Identify the left and right endpoints of each nonzero segment by horizontal coordinate, independently of the original written endpoint order. If it contains , it has exactly one of these roles:
| Role | Exact condition | Active to the right of ? |
|---|---|---|
| Starting member | Left endpoint is ; right endpoint has horizontal coordinate greater than | Yes |
| Ending member | Right endpoint is ; left endpoint has horizontal coordinate less than | No |
| Strictly through member | belongs to the segment, with horizontal coordinate strictly between its endpoints | Yes |
Being nonzero and nonvertical guarantees distinct left and right endpoints, so these three roles do not conflict. In the four-input example, starts, ends, and pass strictly through. All participate in current pairing, but only remain on the right.
How to collect every old active participant
Call the active order immediately left of the “old order.” Its segments through include ending and strictly through members; starters have not entered yet. Call the consecutive portion consisting of these old participants the “meeting block.” With no old participants, it is empty.
Why must they be consecutive? With fewer than two old participants, the block is empty or has one member. Otherwise choose the lowest and highest participants. Any old member between them still exists on reaching : it cannot end in the immediately preceding event-free interval, and even if it ends exactly at , its closed segment includes that endpoint.
Now use Chapter 2's height-difference argument for a segment between two others. At boundary , the middle member cannot be below the formerly lower member. Otherwise their positive height difference on the left becomes negative, forcing an intersection in the preceding event-free interval. Nor can it be above the formerly upper member, for the same reason. Both outside participants have height at , so the height between them must also equal . The middle member passes through too.
Chapter 2 excluded this middle member using its prohibition of three segments meeting. We now allow such meetings, and obtain a new conclusion: the middle member must be included in the meeting block. We cannot reuse Chapter 2's restriction to keep only the first two selected inputs.
The same boundary argument ensures that evaluating every old member's height at yields a nondecreasing bottom-to-top sequence, possibly with a whole block equal to . Otherwise a formerly lower member would become higher here, implying an earlier crossing. We can therefore locate exactly the first old member with height at least , then collect consecutive equal-height members until the height differs. If that first member is already higher, there is no equal-height block, only an insertion gap. This can use ordered search, rather than scanning every input each time.
After collecting the whole block, merge inputs starting at from the list. Ending members should already be in the old block, and the endpoint list also identifies them for checking. Merge by original identifier, keeping each input once. Every nonzero input containing is either already active on the left or starts here, so these two routes cover all nonzero participants. Old members outside the block have heights different from and do not pass through . New inputs are explicitly supplied by endpoint information, so bystanders are not added without cause.
Section 4: Why four participants give six results
The four-input example has participants . Use Chapter 1's method of pairing only with larger original identifiers, listing them in original identifier order:
| Current input | Pairs in this row | Complete intersection |
|---|---|---|
| ; ; | for every pair | |
| ; | for every pair | |
| None | No new result |
There are results. In this table the letters represent four distinct original identifiers in the listed order; they are not renumbered according to current vertical order.
Why are all six pairs genuine? We established that every participant contains , so any two share at least that point. Chapter 1's intersection-shape result states that if two closed segments shared another distinct point, they would share a positive-length interval. Such overlap is excluded in this chapter, so the complete intersection is exactly . This holds for any number of participants, not just four.
Pairing only with larger identifiers makes every pair appear once in this event, excludes self-pairing, and prevents reversed order from creating another pair. There is no duplication across events either: under our conditions, any pair's nonempty intersection is one point, and that exact position is processed once. Conversely, for any real point intersection at the current position, both inputs contain that point. Once all participants are collected, that pair must appear. Thus this step neither misses, invents, nor duplicates results.
This also explains precisely what merging positions means: the list retains one , but the results separately retain all six input-pair identities. Six results must not be compressed into a single statement that an intersection exists here.
Section 5: Zero-length inputs participate here, but do not continue rightward
Now take only , together with two new inputs , each with both endpoints at . Each consists of one point, but they have distinct original identities.
The two figures show separately so overlapping markers do not obscure one another; they have not been moved to different positions. A diamond does not represent an area, and is the same input in both figures.
Treating a point input as both a starter and an ender and simply concatenating the lists could insert its identity twice. Instead define a separate “point input” role: an input with coincident endpoints is kept once by original identifier in that position's list, classified separately from nonzero starters and enders.
On reaching , first collect all nonzero participants, then merge point inputs at this position. The participants here are therefore , giving:
| Original unordered input pair | Complete intersection |
|---|---|
| Point | |
| Point | |
| Point |
Chapter 3 already gave exact point–segment and point–point decisions. What is new here is their place in the sweep: a point input exists only at and has no point at any , so after pairing it does not enter the active order on the right. It cannot be placed above or below , and needs no invented supporting line or slope.
Only exists on both sides in this example. With only and no nonzero segment, their pair must still be reported, while both side orders remain empty. With only , no pair of distinct inputs can be formed, so the result is empty, but the endpoint event is still processed normally.
The same applies when a point lies on a closed endpoint. With only , report for that pair before ends; the point input does not remain on the right. With only , first report their same-point pair, then let continue alone. Multiple point inputs at one position are paired separately with every nonzero participant and with each other by original identifier. Geometric coincidence does not merge identities.
Section 6: Report first, then order the entire right-side meeting block
Return to the event for . Only after all current results have been reported do we update membership: ending member leaves, strictly through members continue, and starting member joins. Any point inputs at this position have also completed pairing, but do not participate in later ordering.
We must not perform an arbitrary chain of pairwise swaps based on which input pair scheduled the event first. What needs ordering is the entire new block of participants continuing to the right.
Use the established slope relation to order both sides
Reuse Chapter 3's distinction between equal height at an intersection and order on either side. For two nonvertical lines through one point with different slopes, the larger slope is above on the right, with the opposite order on the left. This requires only that both segments exist on the side discussed, not that only two segments participate in the event. It can therefore be applied pair by pair to this chapter's meeting block.
Here denote the four supporting lines' slopes. Reading them from gives respectively. The old members are only ; decreasing slope on the left gives bottom-to-top order . The new members are only ; increasing slope on the right gives .
The figures show left-side members and order, followed by right-side members and order. Each draws only the portions actually present on that side; retain this reading order if they wrap. The labels and refer to the immediate sides of the event, not sampling at a fixed decimal offset.
Why do pairwise comparisons determine the whole block? Any two new right-side members pass through and have positive length to the right, so the smaller slope must lie below the larger. Sorting all slopes from small to large simultaneously satisfies every pair's vertical relation. The same reasoning gives the reverse order for old members on the left.
We must also handle equal slopes. Two nonvertical lines through with the same slope are the same line. If both inputs extend rightward, they share a positive-length interval from to the earlier right endpoint, violating our conditions. If both arrive from the left, they similarly overlap on the left. Thus two equal-slope members cannot occur in the same side's block, and no arbitrary tie-break is needed.
However, one input ending here and another starting here may have equal slopes. For example, take another input from to , paired only with . Both satisfy , with ranges and . Their only common horizontal coordinate is , so their complete intersection is . Only exists on the left and only on the right. Pair them first, then replace membership; do not reorder two simultaneously continuing members, because these inputs do not continue together. Endpoint-only contact with different slopes also uses role-based insertion and removal, rather than forcing the swap rule for two through members.
Why order outside the block remains correct
Active members outside the old block do not pass through , so their heights at are strictly below or above . They have no endpoints at this horizontal coordinate either: that would create a distinct event with the same horizontal coordinate, violating this chapter's assumptions. They therefore exist immediately on both sides.
Take any outside member and any new-block member. If their strict vertical order at reversed arbitrarily close on the right, Chapter 2's height-difference sign-change result would force an intersection between those positions. That would create an event in the immediate event-free interval on the right, a contradiction. Thus the new meeting block remains between the original outside members below and above. Outside members neither intersect each other nor enter or leave here, so they retain their order.
There are finitely many inputs and no positive-length overlaps, hence finitely many endpoints and point-intersection positions. Distinct event horizontal coordinates ensure an interval free of other events exists on each side. “Immediately” above refers to such an interval for the proof, not to an algorithm guessing a sufficiently small decimal.
If the old block is empty but there are starters, insert the new block in the gap found at . If the new block is also empty, retain or connect the two sides of the original gap. This covers start-only, end-only, ordinary-crossing, mixed same-point, and pure-point events.
Section 7: Which adjacencies need checking after the update
Write the new block from bottom to top as , where is its member count and its th member. Let denote the outside member immediately below the gap and the one immediately above. These name local positions, not new inputs, and either side may be absent.
When , this short portion of the updated order is
Consider only input pairs consecutive in this sequence:
| Position | Adjacency to examine | Action |
|---|---|---|
| Lower boundary | If the lower member exists, check real intersection and strict future | |
| Inside the new block | through | Every pair already intersects at ; do not schedule again as future |
| Upper boundary | If the upper member exists, check real intersection and strict future |
For there are no internal adjacencies. Any two block members contain , and overlaps are excluded here, so their intersection is only , with no future intersection possible. We can reject future scheduling directly on that basis. A uniform intersection check would also return only the current position, which the strict-future condition must reject. After ordering the new block, do not perform an extra swap for some pair that scheduled the event.
For , removing the old block makes direct neighbors if both exist; check that pair. A missing side means no such new neighboring pair. If the old block was empty too, as in a pure-point event, the sides were already neighbors. There is no new adjacency and a repeated check can be skipped.
Boundary checks continue to use Chapter 3's real-intersection decision and strict-future condition. Empty intersections are not scheduled. A resulting point is future only if its horizontal coordinate is strictly greater than . The current position with coordinate has been handled together and is not re-added; positions to its left are not added either. Existing real future positions remain listed and must not be lost because neighbors changed.
Why are these checks sufficient? Outside members are neither inserted, removed, nor reordered relative to each other, so distant old adjacencies remain. Any new adjacency with both inputs in the new block is an internal pair in the table. With only one input in the block, it must cross the upper or lower boundary. With both inputs outside, the only new possibility is connecting the two sides after removing the old block when the new one is empty. Every case is covered. Unchanged adjacencies need not be rechecked every time: they were checked when formed, and their discovered real future positions remain on the list.
The claim that every new adjacency is checked assumes that the previous order was maintained correctly and previous new adjacencies were checked. This section shows how a same-point update preserves that rule; it does not assume that unreached intersections have already been processed.
Section 8: Why a shared point without an endpoint is also scheduled
In the first three examples, contains an endpoint, so the list knows its position from the start. But if two or more segments all pass strictly through a point, with none starting or ending there, how is it first discovered? Expanding the meeting block on arrival cannot answer this: we still need to explain why arrival happens.
Choose an arbitrary shared point without an endpoint, denoted by to distinguish it from the earlier . At least two segments pass strictly through it. Assume only that events to the left of have been processed correctly:
- Real event positions are finite, so an interval without other events can be chosen to the left of . All inputs through pass strictly through it, so they are already active in that interval.
- Section 3's between-neighbors argument applies at : any active member between two through members must also pass through . These through members therefore form a consecutive block of at least two members on the left.
- Choose any consecutive pair in this block. They are neighbors in the entire active order, not merely after removing bystanders. Look back through the finite earlier events to when this uninterrupted adjacency formed and persisted to the left of . The initial order was empty, so it must have formed at some time, strictly left of .
- By Section 7's classification of all new adjacencies, this pair was checked when it formed. Both inputs contain , and positive-length overlaps are excluded, so their intersection is exactly . At that time was strictly future. The list therefore either added or already contained and retained it.
- Removing the leftmost unprocessed position by horizontal coordinate eventually exhausts the finite events on the left and reaches the retained . On arrival, collect the whole block and report all unordered input pairs, not just the pair that scheduled it.
This does not claim that every pair meeting at a point must previously have been adjacent. At least one earlier neighboring pair suffices to schedule the position; searching on arrival supplies all remaining participants.
Assuming the left side was correctly processed is not circular. If a first real event position were never reached, it could not be a preregistered endpoint. All events on its left would have arrived and been handled correctly by this chapter's participant, pairing, and order rules. The five steps would then apply, showing that it was scheduled and must arrive, a contradiction. This supplies the discovery argument after allowing many segments at a point. Chapter 2's claim that two chosen crossing segments must be neighbors beforehand depended on excluding triple meetings and cannot be copied unchanged.
Section 9: Assemble one same-point event into a complete procedure
We can now put the established rules together. Each row below is a teaching stage for one event position, not an execution trace of a particular implemented function.
| Stage | Action | Why it belongs here |
|---|---|---|
| Take the position | Remove the leftmost unprocessed position and merge same-point endpoint information | Process one exact position together, once |
| Find participants | Locate the old active meeting block exactly, merge starters and point inputs, and deduplicate by original identifier | Neither the scheduled pair nor the endpoint list alone contains all participants |
| Report | Pair distinct original inputs by smaller and larger identifiers and record complete intersections | Enders still contain the current closed endpoint; each same-point input pair has its own result |
| Update | Remove enders, retain through members, add nonzero starters, and order the new block by right-side slope order | Current participants differ from subsequent active members; point inputs do not continue rightward |
| Check | Check internal and boundary adjacencies of the new block; if empty, check the newly connected outside pair | Cover all new adjacencies and schedule only real positions strictly in the future |
An implementation may temporarily remove a state block from a container to locate or reorder it. Provided it retains every participant and first completes the current intersection reports, this is not premature loss of ending identities. This article specifies which information mathematics depends on and when members cease to qualify as participants; it does not infer an order for internal container operations.
Finally, check the current event against each minimal example. Each row contains only the explicitly listed inputs; do not combine separate examples into one set:
| Example inputs | Old bottom-to-top order | Current participants | Current results | New bottom-to-top order |
|---|---|---|---|---|
| for | ||||
| for | ||||
| The six pairs listed in Section 4, all at | ||||
| ; ; , all at | ||||
| Empty | for | Empty | ||
| Empty | None | Empty | ||
| for |
One position is processed once but can yield many retained results. Ending members participate in closed-endpoint pairing before leaving; point inputs retain their own identities; and the right-side order keeps only nonzero members that genuinely continue. Distinguishing these three facts lets starts, ends, and crossings be handled together in one event.
This chapter separately established participant completeness, genuine results reported once, correct post-event order, sufficient new-neighbor checks, and discovery even without endpoint hints. The arguments require no positive-length overlap, nonvertical nonzero inputs, and distinct horizontal coordinates for distinct events. Next, Chapter 5, How overlapping segments participate in the sweep, incorporates overlaps. Chapter 6 handles distinct positions with equal horizontal coordinates, and Chapter 7 introduces shearing. The complete mixed-input sweep, general correctness, and total work are left to Chapters 8–10 respectively.