Skip to main content

How Overlapping Segments Participate in the Sweep

When a sweep line encounters two overlapping segments, they stay at the same height over a range. Which one goes above the other? If a third line crosses the overlap, which input pairs should be reported?

This is Chapter 5 of the segment-intersection series. We remove the restriction against nonzero-length overlaps while continuing to allow the same-point events and point inputs from Chapter 4, "Handle Everything at the Same Point Together". We retain two teaching conditions: nonzero inputs are not vertical, and different event positions have different horizontal coordinates. Several endpoints may share one position. Here event positions are input endpoints and actual intersections on different supporting lines; not every point of an overlap interval is an event.

This article adds only the mechanisms needed for overlap. Original identities and complete intersections follow Chapter 1; exact collinearity tests and closed-interval calculations follow Chapter 3. We now need to decide when to use these results and how to maintain the active order and future events.

Section 1: One position in the order, with every input identity retained inside the group​

Start with three nonzero inputs, all on the line y=0y=0:

InputLeft endpointRight endpoint
A\teachBlue{A}(0,0)(0,0)(6,0)(6,0)
B\teachOrange{B}(2,0)(2,0)(8,0)(8,0)
C\teachViolet{C}(3,0)(3,0)(4,0)(4,0)

When 3<x<43<x<4, all three inputs are active, and the sweep line meets them at the same point. No permutation of their three identifiers makes one geometrically higher than another. Treating them as three distinct height positions would introduce an internal order with no geometric meaning into the notion of "two neighbors."

We place nonzero inputs with the same supporting line in one collinear group. Each nonempty group occupies one position in the active order; within it we separately store the original identities of its currently active inputs. The three inputs above belong to group G0G_0, whose fixed line is y=0y=0.

Input A shown separately: from (0,0) to (6,0), in group G_0 on the line y=0.Input A shown separately: from (0,0) to (6,0), in group G_0 on the line y=0.
Input B shown separately: from (2,0) to (8,0), in the same group as A but retaining its separate identity.Input B shown separately: from (2,0) to (8,0), in the same group as A but retaining its separate identity.
Input C shown separately: from (3,0) to (4,0), nested inside the first two inputs while retaining its own identity.Input C shown separately: from (3,0) to (4,0), nested inside the first two inputs while retaining its own identity.

All figures in this article are teaching diagrams, not actual execution traces. Read the panels in their displayed order; they wrap when space is insufficient, and each retains its original 320 × 260 size. Overlapping members in separate panels have not been shifted to different heights. Both axes use equal units, and OO marks the origin. The text gives the detailed coordinates and processing rules.

Grouping uses the exact relationship between lines, not merely equal slopes, and certainly not visual closeness. The parallel-versus-collinear classification from Chapter 3 identifies the same line; distinct parallel lines belong to different groups.

A group's line does not change with its members, but which members currently belong to it does change. For example, BB belongs to G0G_0, but has not started when x<2x<2 and cannot count as a currently active member. Nor does grouping merge all collinear inputs into a new input: reporting results still requires the original identifiers and endpoints.

Point inputs keep their separate role from Chapter 4. A point does not determine a unique line, so we assign it no invented collinear group and do not retain it in the active order on the right.

Section 2: Report the complete intersection when a new member joins​

In the three-input example, AA starts first. At (2,0)(2,0), BB starts while AA has not yet ended. They are collinear and both contain the current position, so they can be paired now.

The complete-interval result from Chapter 3 gives the intersection of A,BA,B as the interval from (2,0)(2,0) to (6,0)(6,0). Although the sweep has not reached x=6x=6, the two original right endpoints are already known, so all information needed for reporting is available. Sweep progress limits who belongs to the current active set, not access to the complete endpoints of a known input.

Use this order: each new member first reports its complete intersection with every member then in the same group, and only then joins the member set itself. Retain old members ending at the current point until closed-endpoint pairing is complete, then remove them.

Event horizontal coordinateCollinear pairing and new resultsMembers of G0G_0 after the event
00AA starts; no old member to pair withAA
22BB with AA: from (2,0)(2,0) to (6,0)(6,0)A,BA,B
33CC with AA, and CC with BB: each pair intersects from (3,0)(3,0) to (4,0)(4,0)A,B,CA,B,C
44CC ends; do not enumerate old member pairs againA,BA,B
66AA ends; do not report A,BA,B againBB
88BB endsEmpty

These three results are the complete intersections for all three unordered input pairs in this example. A,BA,B overlap partially, while CC is nested inside both. The same joining rule handles all of them; nesting does not need a separate sweep procedure.

Several inputs starting at the same point​

Take a separate example: J1,J2,J3J_1,J_2,J_3 all occupy the segment from (1,0)(1,0) to (5,0)(5,0), but J3J_3 has its endpoints written in reverse order, (5,0),(1,0)(5,0),(1,0). After normalizing left and right endpoints, all three start at (1,0)(1,0) and still have three separate original identities.

Within the same event, process the starters in original-identifier order. J1J_1 joins first. J2J_2 pairs with the already joined J1J_1, then joins. J3J_3 pairs separately with the already joined J1,J2J_1,J_2, then joins. Each of the three pairs retains one complete interval from (1,0)(1,0) to (5,0)(5,0).

The procedure is unchanged if more inputs start at the same point. For any two of them, the earlier joiner becomes a pairing partner of the later joiner. Treating every starter as "not yet in the group" while checking only older members would miss pairs between new members.

Why ending members must stay until pairing finishes​

Take another two-input example containing only BB and a new input EE, where EE runs from (8,0)(8,0) to (10,0)(10,0). At (8,0)(8,0), first pair the starting EE with BB, which has not yet been removed. Their complete intersection is the closed endpoint (8,0)(8,0). Only then remove BB.

This reuses the closed-endpoint processing order from Chapter 4. The new point is that it also applies when members hand over within a group. Collinear pairing may report a point or a nonzero-length interval; it cannot always be described as "outputting an overlap."

Why every intersecting collinear input pair is reported exactly once​

Take any two distinct collinear nonzero inputs Si,SjS_i,S_j. Let ℓi,ri\ell_i,r_i be the horizontal coordinates of the left and right endpoints of SiS_i, and ℓj,rj\ell_j,r_j those of the other input. Being nonzero and nonvertical ensures ℓi<ri\ell_i<r_i and ℓj<rj\ell_j<r_j.

First suppose ℓi<ℓj\ell_i<\ell_j. If the inputs intersect, the closed-range result of Chapter 3 guarantees ℓj≤ri\ell_j\le r_i; otherwise the first would end before the second starts, leaving no shared range. At the event where the second starts, the first has already joined and has not yet left: if ℓj<ri\ell_j<r_i it still passes through, and if ℓj=ri\ell_j=r_i it is retained as an ending member until reporting finishes. This pair is therefore processed when SjS_j joins.

If ℓj<ℓi\ell_j<\ell_i, exchange the earlier and later roles; the same reasoning ensures processing when SiS_i joins. If ℓi=ℓj\ell_i=\ell_j, equal horizontal coordinates on the same nonvertical line mean the same position, so the two are same-point starters. The sequential joining rule above pairs them when the later-processed one joins.

These cases cover every possible start order. Each input starts only once, and later endings or external crossings do not enumerate old collinear member pairs again, so each pair is reported only once. Conversely, an old member paired at joining time shares the new member's line and contains the current point, so their intersection really is nonempty; its complete range is determined by the original endpoints. Thus no result is missed or invented. Disjoint collinear inputs cannot appear together in such a pairing list.

There is no need to build another list of "already output input pairs" to prove uniqueness. This section directly restricts when a pair is generated, instead of generating duplicates first and deleting them later.

Section 3: Expand only cross-group pairs at an external crossing​

Return to A,B,CA,B,C and add the nonzero input D\teachEmerald{D}, from (1,−4)(1,-4) to (9,4)(9,4), on the line y=x−5y=x-5. Also add the point input ZZ, whose endpoints are both P=(5,0)P=(5,0). This section uses only these five inputs.

At PP, DD has height 5−5=05-5=0, and 1<5<91<5<9. Both A,BA,B contain PP too. CC ended at x=4x=4 and must not participate merely because it belongs to G0G_0. DD belongs to another collinear group G1G_1, which currently contains only DD.

A, D, and point input Z shown in one coordinate system. P=(5,0) lies in the interiors of A and D.A, D, and point input Z shown in one coordinate system. P=(5,0) lies in the interiors of A and D.
B, D, and the same point input Z shown at the same scale. P also lies inside B; C has already ended and is not shown.B, D, and the same point input Z shown at the same scale. P also lies inside B; C has already ended and is not shown.

The two panels show collinear A,BA,B separately. In both panels, D,ZD,Z refer to the same respective inputs; displaying them twice creates no extra identities. The diamond identifies the point input, and the dashed line locates x=5x=5; neither is a new segment input.

The method for finding the full meeting block from Chapter 4 now applies first to groups. Use exact height at the current position to find the groups in the old order that pass through PP, combine them with the groups of nonzero inputs starting here, and add the point inputs here. Every old member of an active group covers the current horizontal coordinate; if the group line passes through PP, all those members contain PP. Ending members are still retained, new starters must participate too, and identities that have not started or have already ended must stay out.

However, we cannot copy Chapter 4's rule of "pair every participant with every other participant." That would report A,BA,B again as the point PP, both duplicating the pair and incorrectly replacing its complete interval. Only the following new results are added now:

Pairing categoryNew unordered input pairsComplete intersection
Between different nonzero groupsA,DA,D; B,DB,DPoint PP for each pair
Point input with nonzero participantsA,ZA,Z; B,ZB,Z; D,ZD,ZPoint PP for each pair
Between old collinear membersNone; A,BA,B was reported at x=2x=2Its original complete interval stays unchanged

Together with the three intervals already reported in Section 2, the five-input example has eight results. The other two pairs C,DC,D and C,ZC,Z have empty intersections because the horizontal coordinate 55 of PP lies beyond the right endpoint 44 of CC.

For any number of groups, take each pair of distinct groups once, and pair every current participating member of one group with every current participating member of the other. Taking each group pair once and storing each identity once within its group prevents duplicates at this event. Different groups have different supporting lines but already share the current point, so their complete intersection can only be that point; no longer interval is missed.

Point inputs follow Chapter 4's rules: pair each with every nonzero participant, and pair distinct point inputs at the same position once with each other. The point-with-point branch still runs even when no nonzero group is present. A pair containing a point input cannot have a second intersection point.

This separates nonempty results into three distinct categories: collinear nonzero pairs are reported at joining time; nonzero pairs from different groups at their unique intersection; and pairs containing point inputs at that point. Processing each position once, together with the within-group joining rule, prevents all three categories from repeating across events. Keeping an original overlap interval complete when a third line crosses it follows directly from this choice of reporting times.

Section 4: Why the current farthest right endpoint is enough​

Member identities must be retained, but must we pair every member of two groups to discover a future intersection? First consider how far one nonempty group covers at the current horizontal coordinate tt.

Use the active members after the current event's updates: they have started and their right endpoints lie strictly to the right of tt. Call the chosen nonempty group GaG_a. For each member SiS_i, continue to write ℓi,ri\ell_i,r_i for the horizontal coordinates of its left and right endpoints. Then

ℓi≤t<ri.\ell_i\le t<r_i.

Let RGaR_{G_a} be the maximum of these rir_i in group GaG_a, the horizontal coordinate of its current farthest right endpoint. It is a number, not a new endpoint identity. The member set is finite and nonempty, so some member SjS_j satisfies rj=RGar_j=R_{G_a}.

Every horizontal coordinate uu from tt through RGaR_{G_a} lies within SjS_j: its left endpoint is no later than tt, and t≤u≤RGa=rjt\le u\le R_{G_a}=r_j. This whole portion of the group's line is therefore continuously covered, with no internal gap. Conversely, if u>RGau>R_{G_a}, it exceeds every current member's right endpoint, so no current member covers it.

Thus for strictly future u>tu>t, at least one current member covers it if and only if u≤RGau\le R_{G_a}. This does not mean every member reaches that far. At reporting time we must still expand the actual members present then.

Now take a nonempty group GbG_b neighboring GaG_a, and write RGbR_{G_b} for its current farthest right endpoint's horizontal coordinate. Neighboring means that no other group lies between them in the active group order. Distinct parallel lines have no candidate intersection. Otherwise use Chapter 3's exact test to obtain the unique candidate Q=(xQ,yQ)Q=(x_Q,y_Q), and schedule QQ only when

t<xQ,xQ≤RGa,xQ≤RGbt<x_Q,\qquad x_Q\le R_{G_a},\qquad x_Q\le R_{G_b}

all hold. The second and third conditions each guarantee that at least one current member of that group really contains QQ; checking only one group is insufficient. The scheduled point is therefore an intersection of two actual inputs, not a false position on extended lines. Equality with a group's farthest right endpoint is allowed because endpoints are closed; equality with the current horizontal coordinate is not strictly in the future.

This explains the division between "discovering a position" and "reporting all member pairs": discovery uses the fixed lines and current farthest right endpoints of two groups, while reporting expands members on arrival. A confirmed real future position remains on the existing pending list and is not canceled when neighbors change.

This reasoning requires every included member to cover tt already. A collinear input that has not started cannot increase the current farthest right endpoint, however far its right endpoint lies. Without shared coverage of tt, doing so could incorrectly fill a gap.

Section 5: Recheck extended coverage even when neighbors stay the same​

Take a new three-input example, independent of all earlier examples:

InputLeft endpointRight endpointSupporting line
F\teachBlue{F}(0,0)(0,0)(2,0)(2,0)y=0y=0
G\teachOrange{G}(1,0)(1,0)(7,0)(7,0)y=0y=0
H\teachEmerald{H}(−1,5)(-1,5)(6,−2)(6,-2)y=4−xy=4-x

Here F,GF,G are input names; their group is G2G_2, and the group containing HH is G3G_3. Do not confuse input GG with a group name. The candidate intersection of the two fixed lines is Q1=(4,0)Q_1=(4,0).

At x=0x=0, FF starts and G2G_2 becomes a neighbor of G3G_3. At this time G2G_2 contains only FF, whose farthest right endpoint is 22. The candidate's horizontal coordinate 4>24>2, so it cannot be scheduled.

At x=1x=1, GG starts and reports its complete intersection with FF, from (1,0)(1,0) to (2,0)(2,0). After it joins, the farthest right endpoint of G2G_2 extends from 22 to 77. The two groups remain the same neighbors, but now 1<4≤71<4\le7 and also 4≤64\le6. We must therefore recheck and schedule Q1Q_1 now.

After the update at x=0, G_2 contains only F and covers through x=2. Its line meets the supporting line of H at Q_1=(4,0), but F does not yet cover the candidate.After the update at x=0, G_2 contains only F and covers through x=2. Its line meets the supporting line of H at Q_1=(4,0), but F does not yet cover the candidate.
After the update at x=1, G joins G_2, extending coverage through x=7. Only the new member G and H are drawn; adjacency is unchanged, but both groups now cover Q_1.After the update at x=1, G joins G_2, extending coverage through x=7. Only the new member G and H are drawn; adjacency is unchanged, but both groups now cover Q_1.

Read the two panels as "before extension, after extension." The second draws GG alone to make the extension clear; it does not mean that FF has already left at x=1x=1. The hollow circle marks only the candidate position on the fixed lines. Whether it has been scheduled depends on the coverage checks in the text.

Copying Chapter 4's conclusion that unchanged adjacencies need no recheck would miss G,HG,H here. In Chapter 4, each position in the order represented one input with fixed endpoints. Now it represents a group whose line is fixed but whose current coverage can grow. This is the new condition missing from the old conclusion.

In general, a group's coverage can extend rightward only when a new member joins. Whenever its farthest right endpoint increases, recheck it against its updated neighbors above and below. If its neighbors change too, also check the new adjacencies. If the joiner does not extend the farthest right endpoint and adjacency stays unchanged, neither the candidate lines nor the two groups' coverage limits gain any usable range. Thus no candidate previously excluded by range can newly pass for this reason.

A short member ending need not end the group​

Continue this example. At x=2x=2, FF ends but GG remains, and the farthest right endpoint is still 77, so G2G_2 does not leave. At Q1Q_1, the current participants are G,HG,H, and their point result is reported. F,HF,H is not reported because FF has already ended.

This example has only two results: F,GF,G has the complete interval from (1,0)(1,0) to (2,0)(2,0), and G,HG,H has the point (4,0)(4,0). At x=6x=6, HH ends. At x=7x=7, GG ends, and only then does G2G_2 become empty.

This is not limited to the numerical example. If a group member ends at tt but the group remains nonempty after the update, at least one other member has a right endpoint greater than tt. The member that just ended reaches only tt, so it cannot extend farther than that continuing member. Removing ending members therefore cannot shorten the farthest right endpoint of a group that remains nonempty. A group leaves only if all members have ended and no new member joins; then check the outer neighbors newly joined across its former position.

Section 6: An empty group cannot keep covering across a gap​

Take another independent example:

InputTwo endpoints
I\teachBlue{I}(0,0),(1,0)(0,0),(1,0)
J\teachOrange{J}(3,0),(7,0)(3,0),(7,0)
K\teachEmerald{K}(−1,3),(6,−4)(-1,3),(6,-4)
Point input WWBoth endpoints are Q2=(2,0)Q_2=(2,0)

I,JI,J lie on the same line y=0y=0, but have a gap between them. KK satisfies y=2−xy=2-x and intersects that line at Q2Q_2. Neither of I,JI,J contains Q2Q_2.

I and J remain on their original line y=0, with no collinear member between x=1 and x=3. K and point input W intersect at Q_2=(2,0).I and J remain on their original line y=0, with no collinear member between x=1 and x=3. K and point input W intersect at Q_2=(2,0).

At x=0x=0, the horizontal group contains only II, with farthest right endpoint 11, so the candidate at horizontal coordinate 22 cannot be scheduled. At x=1x=1, II ends, the group becomes empty, and it leaves the active order. Knowing that the collinear JJ will start later does not allow us to extend coverage to 77 in advance.

At x=2x=2, point input WW supplies its own endpoint information, so this position is still visited. Only K,WK,W are paired here, with intersection Q2Q_2; the horizontal group does not participate. At x=3x=3, JJ starts and its group reenters the active order with JJ as its only current member. Its supporting-line intersection with KK is already to the left and does not lie on JJ; reactivation cannot turn it into a new result. The only unordered input pair with a nonempty intersection in this example is K,WK,W; the other five pairs have empty intersections.

The same holds for any gap. When a group has no members, no original input can justify coverage of that range, so the group must leave. When its first new member later starts, reinsert the group at that start event and check adjacency. The group record may remember "this is the same line," but cannot claim that an active input has continuously occupied it.

Section 7: Extend the reasons for ordering and event discovery to groups​

One key question remains: are these rechecks enough to discover cross-group intersections without endpoint reminders? We cannot arbitrarily choose two "neighbors" among overlapping members: members of the same group share a line, so there is no unique line intersection to schedule. We must reason about groups instead and check when their coverage meets the required conditions.

A contiguous meeting block and the order on either side​

Within an event-free gap, every nonempty group has members covering that gap, and its height is determined by its fixed line. If different groups have equal height, they actually intersect there; distinct parallel groups never have equal height. Thus Chapter 2's conclusion that order cannot change without an event applies between distinct groups. Member starts and ends are already endpoint events and cannot occur unnoticed inside the gap.

At an event point, the old groups passing through it still form a contiguous block. Take the lowest and highest participating groups. A group between them has an active member immediately to the left, and that member cannot end in the event-free gap. Even if it ends at the current point, its closed endpoint is still retained. The height-sandwich result cited in Chapter 4 then applies to these different group lines, forcing the middle group through the current point too. With zero or one old participating group, contiguity holds directly.

First find all these groups, then combine same-point starters and point inputs, and report according to the three categories above. After reporting, remove ending members and empty groups, retain continuing groups, and add newly activated groups. Point inputs do not participate in the right-side order.

Different groups passing through the current point must have different slopes. If their slopes agreed and they passed through the same point, their lines would coincide and they would belong to one group. We no longer use "overlap is forbidden" to exclude equal-slope inputs; we have already placed them in one group. We can therefore directly use Chapter 3's slope relationship on either side to arrange nonempty groups on the right in increasing slope order.

Chapter 4's reason the order outside the block stays correct still applies. Groups outside the block do not pass through the current point and are strictly higher or lower there. Different positions have distinct horizontal coordinates, so those groups do not start or end separately here either. Place the new group block between the same outer groups above and below; all other positions retain their order.

Two kinds of change must be checked after updating​

The first is new adjacency. Chapter 4's classification into the new block's interior, upper and lower boundaries, and the connection across an empty block now applies to groups. Distinct groups inside the block have already intersected at the current point; their fixed lines cannot have a second intersection, so there is no strictly future internal candidate. Check new adjacencies at the boundaries and across an empty block using the two-group coverage conditions from Section 4.

The second is unchanged adjacency with extended coverage in a group. As Section 5 showed, its current neighbors must be rechecked. Outside the groups affected by endpoints or intersections, both members and adjacency remain unchanged, so no third kind of change requires rechecking. A group still nonempty after endings does not shorten its farthest right endpoint; adjacency changes caused by an empty group leaving already belong to the first kind.

Why an intersection without endpoint reminders is scheduled in time​

Take an actual cross-group intersection Q∗=(x∗,y∗)Q_*=(x_*,y_*) with no input endpoint at it. For now, assume only that the events to its left have been processed correctly.

There are finitely many inputs and endpoints, and each pair of fixed lines from different groups has at most one intersection. Thus the event positions requiring stops are still finite, even though an overlap interval contains infinitely many points. We can therefore choose a gap immediately to the left of Q∗Q_* containing no other events. Every nonzero input containing Q∗Q_* has already started and has not yet ended there.

The groups containing these inputs form a contiguous block in the old order, with at least two distinct groups. Choose neighboring groups in this block and call them Ga,GbG_a,G_b. The unique intersection of their two fixed lines is Q∗Q_*.

Look back along the finitely many events on the left to when this adjacency formed and then persisted up to the left side of Q∗Q_*. It was checked when it formed, but we cannot immediately assert that coverage already reached Q∗Q_* then. This may be exactly the case from Section 5 where the candidate was initially excluded by range.

There are therefore two cases:

  • If both groups' current farthest right endpoints were at least x∗x_* when the adjacency formed, that check scheduled Q∗Q_*.
  • If at least one group then had insufficient coverage, but immediately to the left of Q∗Q_* both have members containing Q∗Q_*, the insufficient coverage must have changed during this uninterrupted adjacency. A fixed member's right endpoint cannot move on its own; both groups remained nonempty, and removing ending members cannot extend coverage either. Thus a new member must have joined and extended a farthest right endpoint. Consider the joining event at which both groups' coverage first simultaneously reached x∗x_*. The recheck rule checks these still-neighboring groups and schedules Q∗Q_*.

Both checks occur strictly to the left of Q∗Q_*: the adjacency already exists on the left, and Q∗Q_* has no endpoint that could delay the needed start event until this position. The candidate is an actual intersection, strictly in the future and covered by both groups, so the scheduling conditions do not reject it. If already recorded, it remains recorded. Chapter 2's proof of retaining positions and taking them in order ensures that it will subsequently be visited.

Overlap within one group does not need this cross-group discovery proof. Each intersecting pair is reported at its later start event, with same-point starters handled by the internal event order, and all start endpoints are registered in advance. Results involving point inputs also have those points' own endpoint reminders. This covers how all three result categories above are reached.

Finally, reuse Chapter 4's earliest missed position argument. If there were an earliest missed position, it could not be an already registered endpoint. Events to its left would have been processed correctly under this chapter's group-update rules, so the cross-group discovery reasoning above would apply, proving that it was scheduled and would be visited. The contradiction is resolved by this chapter's added step: "also recheck when coverage becomes sufficient during a continuing adjacency." It does not assume in advance that the target intersection has already been processed.

This chapter thus connects reporting and discovery for overlaps separately. Collinear pairs generate their complete results only at joining time; cross-group events expand actual members. Group ordering avoids meaningless sorting within one line, the current farthest right endpoint supports candidate checks, and both coverage extension and gaps are handled by event rules. We neither turn every event into another enumeration of all old collinear member pairs nor expand every pair of members of two groups just to discover a position.

These are local mechanisms and their justifications under this chapter's conditions. The next article, Chapter 6, "Equal Horizontal Coordinates Do Not Mean a Shared Reordering", handles different positions with the same horizontal coordinate, and Chapter 7 introduces shear. The full mixed sweep, combined correctness argument, and total-work analysis remain for Chapters 8 through 10 respectively.