Skip to main content

Keeping geometric decisions exact

Chapter 2, Why no intersection is missed, proved why checking only new neighbors is enough. Every step, however, depends on correct decisions: where to insert a new segment, whether a candidate point really lies on a segment, which event to process first, and whether an intersection has already been registered. If a calculation gets one of these relationships wrong, the execution may no longer satisfy the proof's assumptions.

Chapter 1, What exactly are we computing?, also left a question open: how do we compute the complete intersection from the endpoints of any two inputs? This chapter first connects the line equations, intersection formulas, and interval results already established into a mathematical procedure we can follow step by step. It then uses the exact results to decide equality, event order, and vertical order during the sweep.

The two-input intersection procedure in the first five sections accepts points, vertical segments, and overlaps. The later connections to the sweep still use Chapter 2's simplifying assumptions: segments have positive length and are not vertical; there are no overlaps, endpoint contacts, or points shared by several lines; distinct events have distinct horizontal coordinates. Computing the intersection of complex inputs and incorporating those inputs into a complete sweep are two separate tasks.

Section 1: First determine whether an input is a point​

Take two distinct original inputs, denoted by S1,S2S_1,S_2. Call the endpoints of S1S_1 A1,B1A_1,B_1, and those of S2S_2 A2,B2A_2,B_2. The subscripts distinguish the inputs; the order in which endpoints are written does not specify a direction. Write xA1,yA1x_{A_1},y_{A_1} for the horizontal and vertical coordinates of A1A_1, and use the same notation for the other endpoints.

This chapter performs exact arithmetic on given rational coordinates. For exact representations of fractions and how they differ from finite-decimal approximations, see the discussion of exact arithmetic at the end of Why shearing preserves intersections. We use this result: calculations retain exact values rather than first truncating them to approximate decimals. If measurements were already approximated before being supplied as input, exact computation here cannot recover the lost information.

First compare the two endpoints of each input. They are the same point only when both horizontal coordinates and both vertical coordinates are equal. This gives three cases: both inputs are points, exactly one is a point, or neither is a point.

This step must come before writing line equations. Writing a line equation from two endpoints requires distinct endpoints: coincident endpoints do not determine a unique line. Identifying point inputs first ensures that every later use of a formula meets its conditions.

Section 2: When an input is a point, check where it belongs​

When both inputs consist of a single point, compare their positions directly: if the two coordinates are respectively equal, their intersection is that point; otherwise it is empty. This applies Chapter 1's point-intersection rule to arbitrary coordinates, without solving equations.

When exactly one input is a point, denote it by T=(xT,yT)T=(x_T,y_T). Temporarily call the distinct endpoints of the other, non-point input E=(xE,yE)E=(x_E,y_E) and F=(xF,yF)F=(x_F,y_F). Here E,FE,F refer to that input's existing endpoints, not newly added inputs. Use the same checks below regardless of whether the point input was listed first or second.

First check whether TT lies on line EFEF. Using the result from the topic on orientation and collinearity, compute

ωEF(T)=(xF−xE)(yT−yE)−(yF−yE)(xT−xE).\omega_{EF}(T)=(x_F-x_E)(y_T-y_E)-(y_F-y_E)(x_T-x_E).

ωEF(T)\omega_{EF}(T) abbreviates this whole expression; the TT in parentheses identifies the point being checked. Provided E≠FE\ne F, its value is zero if and only if E,F,TE,F,T are collinear. The earlier topic explains positive, negative, and zero values; here we use only the zero case.

If the result is nonzero, TT does not lie on the supporting line, so the intersection is empty. If it is zero, we still need a range check. Use the segment-range result from the line-equation topic:

  • If xE≠xFx_E\ne x_F, require xTx_T to lie between the endpoints' horizontal coordinates, including both ends.
  • If xE=xFx_E=x_F, this non-point segment is vertical. Require yTy_T to lie between the endpoints' vertical coordinates, including both ends.

Here “between two numbers” does not depend on the order in which the endpoints are written. For example, the first condition can be written as

min⁡(xE,xF)≤xT≤max⁡(xE,xF),\min(x_E,x_F)\le x_T\le\max(x_E,x_F),

where min⁡\min selects the smaller value and max⁡\max the larger. For a vertical segment, replace these three horizontal coordinates with their corresponding vertical coordinates.

Both collinearity and range checks must pass. Collinearity excludes points off the line; the range check excludes parts of the line beyond the endpoints. Together they establish that the point belongs to the closed segment. If both pass, the intersection is TT; otherwise it is empty. Using “less than or equal to” at both boundaries ensures that a point exactly at an endpoint is not discarded.

We will use this point-membership check again. The only difference will be whether the point comes directly from an input or is a computed candidate intersection.

Section 3: If neither input is a point, first compare the supporting lines​

We now know that A1≠B1A_1\ne B_1 and A2≠B2A_2\ne B_2, so line equations are valid. For i=1,2i=1,2, with ii taking each input's number in turn, use the endpoint-to-coefficient result to compute

ai=yBi−yAi,bi=xAi−xBi,a_i=y_{B_i}-y_{A_i},\qquad b_i=x_{A_i}-x_{B_i}, ci=aixAi+biyAi.c_i=a_i x_{A_i}+b_i y_{A_i}.

These give the two supporting lines aix+biy=cia_i x+b_i y=c_i. Here ai,bi,cia_i,b_i,c_i are fixed numbers calculated from the endpoints, while x,yx,y are coordinates of a point on a line. Each pair ai,bia_i,b_i cannot both be zero. This chapter uses the given coordinates directly, without scaling, so we consistently use lowercase x,yx,y.

Next compute

Δ=a1b2−a2b1.\Delta=a_1b_2-a_2b_1.

Δ\Delta, pronounced “delta,” names this difference of products. The topic on finding intersections by elimination already explains its role: when Δ≠0\Delta\ne0, the two lines have a unique intersection; when Δ=0\Delta=0, they are parallel or coincident, and the division below is not allowed. The elimination and line classification there require only valid line coefficients, not integer coefficients. Integers are needed for the later denominator bound, which we do not use here.

Nonzero denominator: Find the candidate point, then check its range​

When Δ≠0\Delta\ne0, call the unique intersection P=(xP,yP)P=(x_P,y_P) and use the formulas already derived:

xP=c1b2−c2b1Δ,yP=a1c2−a2c1Δ.x_P=\frac{c_1b_2-c_2b_1}{\Delta},\qquad y_P=\frac{a_1c_2-a_2c_1}{\Delta}.

For now, this is only a candidate for the segments' intersection. The formulas guarantee that it lies on both lines, but not that it lies between both pairs of endpoints. Apply Section 2's range check separately to S1,S2S_1,S_2. Check horizontal coordinates for a nonvertical input and vertical coordinates for a vertical input. Keep PP only if both pass.

Why can we declare the intersection empty as soon as either check fails? Any common point of the segments must also be a common point of their supporting lines, and PP is the only common point of those lines. If either segment excludes PP, no other candidate can exist elsewhere. Conversely, if PP passes both range checks, it belongs to both segments, so their intersection is exactly that point.

Horizontal and vertical inputs have not been excluded. A vertical line makes its own bi=0b_i=0, but does not necessarily make Δ=0\Delta=0. Whether the unique-intersection formula applies is always decided by the entire Δ\Delta. Valid endpoint contacts are also retained because the range checks include both ends.

Zero denominator: Distinguish parallel lines from a shared line​

When Δ=0\Delta=0, stop using the unique-intersection formula. Substitute endpoint A2A_2 of the second input into the first line, and check whether

a1xA2+b1yA2=c1a_1x_{A_2}+b_1y_{A_2}=c_1

holds.

Why is one endpoint enough? The condition Δ=0\Delta=0 has already restricted the lines to being parallel or coincident. A2A_2 lies on the second line; if it also lies on the first, the lines have a common point. This rules out distinct parallel lines, which never meet, leaving coincident lines. If A2A_2 is not on the first line, the lines cannot coincide. They are distinct parallel lines, so the segments' intersection is empty.

The assumption Δ=0\Delta=0 is essential here. Two lines with a unique intersection may also share an endpoint of the second line. One point lying on a line alone does not establish that the two lines coincide.

Section 4: For collinear inputs, select the complete interval from original endpoints​

Both inputs are now known to be non-points on the same supporting line. Reuse Chapter 1's rule for selecting the complete interval's endpoints: order by horizontal coordinate if the common line is nonvertical, and by vertical coordinate if it is vertical. Identify each input's earlier and later endpoint, then select the later of the earlier endpoints and the earlier of the later endpoints.

Continue to call the selected points L,RL,R. Equality in the ordering coordinate means the same point on this common line: on a nonvertical line, each horizontal coordinate identifies one point; on a vertical line, each vertical coordinate identifies one point. Comparing L,RL,R therefore gives all three possibilities:

Order along the common lineComplete intersection
LL is after RREmpty
LL and RR are the same pointPoint LL
LL is before RRThe complete closed interval of positive length from LL to RR

Chapter 1 proved that these two boundaries delimit exactly the common part. Here we place that result within the intersection procedure: establish collinearity before comparing ranges along one line. Overlapping horizontal-coordinate ranges alone do not imply that segments on different lines overlap.

Both boundaries are chosen directly from input endpoints; there is no need to force an intersection calculation on equations with Δ=0\Delta=0. Completely coincident inputs yield their entire common segment. Reversing the written order of endpoints does not change the ordered boundaries. The output describes the whole interval by L,RL,R, rather than merely recording two isolated points or splitting the interval where another input passes through it.

Section 5: Check that the procedure covers every case​

We can now proceed from four given endpoints to a definite result. The table below checks the procedure; every condition in it was established above.

Case identified firstNext checkResult
Both inputs are pointsAre both coordinates respectively equal?That point if equal; otherwise empty
Exactly one input is a pointIs it collinear with and within the closed range of the non-point input?That point if both hold; otherwise empty
Both are non-points, Δ≠0\Delta\ne0Is the unique candidate in both closed ranges?The candidate if both hold; otherwise empty
Both are non-points, Δ=0\Delta=0Does an endpoint lie on the other supporting line?Empty if not; otherwise continue to the collinear case
Both are non-points and collinearCompare the selected L,RL,REmpty, a point, or a complete interval of positive length

Each input has either identical or distinct endpoints, so the first level omits nothing. Once both are non-points, Δ\Delta is either zero or nonzero. If zero, the equation obtained by substituting the endpoint either holds or does not. Finally, the collinear branch covers before, same point, and after. Every path ends with an empty set, a point, or a complete interval; no case requiring separate treatment is hidden inside an invalid fraction.

The result remains attached to the original unordered input pair S1,S2S_1,S_2 selected at the start. Identical geometric positions do not erase input identities, and swapping the inputs does not create a new pair. This is a teaching procedure for organizing mathematical decisions, not the call sequence of a particular implemented algorithm function.

Section 6: Keep using intersection results in later decisions​

After finding a point, the sweep must compare it with endpoints, existing events, and the current position. Exactness must be preserved throughout this chain of calculations. If we compute an exact intersection but replace its coordinates with approximations before registration, the event list is no longer reasoning about the point just computed.

Why can the formulas continue to use rational numbers? Endpoint coordinates are rational. Coefficients and decision expressions use only addition, subtraction, and multiplication; the unique-intersection branch also divides by Δ\Delta, already confirmed nonzero. These operations still produce rational numbers. The collinear branch selects original endpoints directly, introducing no different kind of number. Computed point coordinates and original endpoint coordinates can therefore use the same exact comparisons.

We compare numerical values. See the intersection-denominator topic for how making denominators positive and reducing fractions preserves value. Different written fractions need not represent different positions. Below we use exact equality, order, and sign results without repeating the basics of fraction arithmetic.

First connect the intersection check to a sweep action. Under Chapter 2's assumptions, the nonempty intersection of a neighboring pair can only be a point. The first five sections either produce that point or determine that the intersection is empty.

  • If it is empty, do not register a crossing for this pair.
  • If a point is obtained, next check whether it lies strictly in the future and whether it has already been registered.

This step decides which positions are eligible to enter the list. Mistaking an intersection of extended lines for a segment intersection registers a crossing that does not exist. Swapping the segments on reaching it would make the maintained order diverge from the actual geometric order. Conversely, if a real intersection is incorrectly declared empty, even perfectly accurate later sorting cannot process a record that was never registered. Thus Chapter 2's arguments against extra reports and missed intersections both require correct membership decisions here.

Section 7: What event order and same-point recognition compare​

Suppose the list contains two distinct event positions, H=(xH,yH)H=(x_H,y_H) and K=(xK,yK)K=(x_K,y_K). They may come from input endpoints or computed intersections. Chapter 2 processes events in increasing horizontal coordinate, so an exact comparison of xH,xKx_H,x_K decides which comes first: process HH first if xH<xKx_H<x_K, and KK first if xK<xHx_K<x_H. We retain the assumption that distinct events have distinct horizontal coordinates, without adding rules for equal-horizontal-coordinate events.

This comparison decides where the sweep stops next, not which intersection was discovered first. Processing an event farther right before one farther left would mean that, at the right event, insertions, removals, or swaps that should have occurred to its left are still unfinished. The earliest-missed-intersection argument would lose its premise that earlier events were processed correctly and the current order matches the geometry.

Another task is recognizing a position discovered more than once. Keep the candidate P=(xP,yP)P=(x_P,y_P) from Section 3, and call the list position being checked Q=(xQ,yQ)Q=(x_Q,y_Q). Being the same point requires both coordinates to be respectively equal:

P=Q⟺xP=xQ and yP=yQ.P=Q\quad\Longleftrightarrow\quad x_P=x_Q\ \text{and}\ y_P=y_Q.

The symbol ⟺\Longleftrightarrow means that the two conditions are equivalent in both directions. Comparing only horizontal coordinates generally establishes only that two points lie on the same vertical line. Vertical coordinates must also be compared to establish the same position.

Under Chapter 2's assumptions, the same input pair may become neighbors more than once. After computing PP again, keep the existing record if the same crossing is already listed; do not add a second one. Exact equality lets both calculations recognize the same position consistently. Mistaking one crossing for two positions can cause duplicate reports and duplicate swaps. Mistaking distinct events for one position can instead lose an event that should have been processed.

Position identity and input-pair identity remain separate information. Same-point comparison answers only the position question. Chapter 1's result about several input pairs at one position still applies: for complex inputs, different pairs retain their own intersection results even if they produce identical coordinates. We do not merge them into a single result here, nor specify how multiple inputs at one point jointly update the sweep state. That is Chapter 4's task.

Section 8: Why strictly future must exclude equality​

Write x0x_0 for the current event's horizontal coordinate. For the computed point PP, which has passed both range checks, calculate

fP=xP−x0.f_P=x_P-x_0.

fPf_P is the candidate point's horizontal coordinate minus the current coordinate; the subscript identifies PP as the point being checked. Its sign directly determines registration:

Exact resultPosition relative to the current eventRegister as a future crossing?
fP>0f_P>0Strictly to the rightAdd if not registered; otherwise retain the existing record
fP=0f_P=0Same horizontal coordinate as the current positionDo not add as a future crossing
fP<0f_P<0To the left, already passedDo not add

Under these sweep assumptions, distinct events cannot share a horizontal coordinate, so equality cannot conceal a different event still awaiting processing. It means that the current position cannot be treated as future. Once this assumption is removed, this table alone cannot decide the required handling.

Why not change the condition to fP≥0f_P\ge0? Immediately after processing the crossing at PP, we have x0=xPx_0=x_P, so fP=xP−xP=0f_P=x_P-x_P=0. If that crossing is tested for registration again, a rule including equality would let it re-enter the future list. When this pair is checked later, the intersection is already to the left and must likewise not be registered again. Chapter 2's no-duplicate-reporting result relies on precisely this strict-future condition.

Conversely, approximating a genuinely positive fP>0f_P>0 as zero would miss an intersection not yet reached. However small the distance, the point is logically still in the future. This error breaks the step in the earliest-missed-intersection argument that checks a pair when adjacency forms and therefore registers its future intersection.

The same xPx_P is used both in this future test and in the previous section's list ordering. The first keeps new records to the right of the current position; the second processes the leftmost listed position first. Keeping the same exact value in both places ensures that eligibility for insertion and the time of removal refer to the same position.

Section 9: Connect line equations to the active order​

Now consider another action: when a segment starts, it must be inserted at the correct bottom-to-top position to find its true neighbors above and below. Heights must be compared at the same horizontal coordinate. Using a different endpoint height from each segment does not represent their relative heights where one sweep line meets both.

Continue to use S1,S2S_1,S_2 and the coefficients ai,bi,cia_i,b_i,c_i computed in Section 3. This section requires both segments to be nonvertical, so bi=xAi−xBi≠0b_i=x_{A_i}-x_{B_i}\ne0. Choose a horizontal coordinate xx covered by both segments, and write yi(x)y_i(x) for the height of SiS_i there. The subscript identifies the input; the parentheses specify where its height is measured.

Reuse the nonvertical-line form from the elimination topic to obtain heights directly from the same coefficients:

yi(x)=ci−aixbi,i=1,2.y_i(x)=\frac{c_i-a_i x}{b_i},\qquad i=1,2.

This is the result obtained there by rearranging aix+biy=cia_i x+b_i y=c_i and dividing by nonzero bib_i. Its new use here is to substitute the comparison position xx and reuse the intersection coefficients for the active order, rather than fitting a separate approximate line. The horizontal coordinate must lie within both segments' ranges; otherwise the calculated height belongs only to an extended line.

Following Chapter 2's height-difference convention, always subtract the first height from the second. Denote the difference by d21(x)d_{21}(x):

d21(x)=y2(x)−y1(x)=c2−a2xb2−c1−a1xb1.d_{21}(x)=y_2(x)-y_1(x) =\frac{c_2-a_2x}{b_2}-\frac{c_1-a_1x}{b_1}.

The subscript 2121 reminds us of the subtraction order. A positive value means S2S_2 is above S1S_1, a negative value means it is below, and zero means equal heights at the same horizontal coordinate. Compute both heights exactly, then take the sign of their difference. A positive numerator alone does not justify ignoring a possibly negative denominator.

At a start event, the new segment's endpoint is not on any other segment, by Chapter 2's exclusion of endpoint contacts. Its height therefore differs from those of existing active segments, letting us locate its insertion position. If even one above/below comparison is reversed, the maintained order may separate a truly neighboring pair. Faithfully checking only recorded new neighbors would then no longer guarantee checking the geometric new neighbors. The geometric proof still holds, but the maintained list no longer meets its assumptions.

At an intersection, equal heights do not determine the order on either side​

If the true intersection of S1,S2S_1,S_2 is PP, substituting xPx_P gives d21(xP)=0d_{21}(x_P)=0. This correctly identifies equal heights, but that zero alone cannot tell us which segment is above after the crossing.

Chapter 2 already proved that order reverses across a crossing. We use that result here: for two nonvertical lines through one intersection with different slopes, the sign of the height difference to the right equals the sign of the second slope minus the first; on the left, it is opposite. This follows from the previously derived product of slope difference and horizontal displacement from the intersection, so we do not repeat the derivation.

The slopes also come from the same coefficients. Again using the elimination topic's notation, write

mi=−aibi,i=1,2,m_i=-\frac{a_i}{b_i},\qquad i=1,2,

where mim_i is the slope of the supporting line of SiS_i. Comparing m2−m1m_2-m_1 exactly checks the vertical order to the right of an ordinary crossing: if positive, S2S_2 is above; if negative, it is below. The two distinct lines here already have a unique intersection, so the slope difference cannot be zero. This crossing result does not apply to collinear overlap.

Chapter 2's sweep rule can directly swap the two adjacent segments whose order was known before the crossing. The slope comparison here shows that the resulting order can also be checked with exact quantities. We need not sample by arbitrarily adding a fixed decimal to the intersection's horizontal coordinate: that sampled position might not precede the next event. Relative order with other active segments follows Chapter 2's local-swap result. This section introduces no handling of distinct events at the same horizontal coordinate.

A vertical segment has bi=0b_i=0, so this section's height formula cannot be used for it. Its two-input intersection was handled in the first five sections; incorporating it into the sweep remains the task of the later shear chapter.

Section 10: Exact decisions support the proof, but also cost work​

We can now match this chapter's calculations with Chapter 2's correctness assumptions:

Relationship preserved by the calculationSweep action and proof premise it supports
The intersection really belongs to both closed segmentsRegister only real crossings and do not wrongly exclude real intersections
Horizontal-coordinate order of distinct eventsProcess left events first, then use the updated active order
Both coordinates are respectively equalDo not register a rediscovered crossing twice; record position separately from input-pair identity
The intersection's horizontal coordinate is strictly greater than the current coordinateNewly registered events remain in the future; do not re-add crossings already reached or passed
Height differences at one position and order on either side of a crossingMaintain the true geometric order after insertion or swapping, and find the new neighbors that need checking

These relationships work together. Accurate intersections give listed positions a geometric basis. Accurate ordering, same-point checks, and future checks allow records to be retained and removed as intended. Accurate active order lets the new-neighbor rule act on the pair used in the proof. Exact arithmetic makes calculations agree with mathematics for the given rational inputs, but correct conditions and sweep rules are still required; arithmetic does not replace their proof.

Chapter 1 already distinguished the number of checks from actual running time. Apply that warning here: costs occur not only when computing intersections. List comparisons and height comparisons also use the resulting numerators and denominators. Our formulas do not fix the number of digits in these integers. Storing longer numbers and performing arithmetic and comparisons on them all take work. Reducing the number of input pairs checked therefore does not give every exact decision a fixed real-world cost. Chapter 10 will analyze number lengths, list maintenance, checks, and output together.

We have now fulfilled Chapter 1's promise of a procedure from arbitrary endpoints to an empty set, a point, or a complete interval, and connected Chapter 2's mathematical decisions to exact calculated quantities. The next chapter considers unified handling of several inputs meeting at one point. Chapter 5 incorporates overlaps into the sweep, Chapter 6 treats local reordering at distinct events with the same horizontal coordinate, and Chapter 7 introduces shearing. Each continues to use this chapter's exact results while adding its required sweep rules.