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 . Call the endpoints of , and those of . The subscripts distinguish the inputs; the order in which endpoints are written does not specify a direction. Write for the horizontal and vertical coordinates of , 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 . Temporarily call the distinct endpoints of the other, non-point input and . Here 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 lies on line . Using the result from the topic on orientation and collinearity, compute
abbreviates this whole expression; the in parentheses identifies the point being checked. Provided , its value is zero if and only if are collinear. The earlier topic explains positive, negative, and zero values; here we use only the zero case.
If the result is nonzero, 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 , require to lie between the endpoints' horizontal coordinates, including both ends.
- If , this non-point segment is vertical. Require 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
where selects the smaller value and 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 ; 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 and , so line equations are valid. For , with taking each input's number in turn, use the endpoint-to-coefficient result to compute
These give the two supporting lines . Here are fixed numbers calculated from the endpoints, while are coordinates of a point on a line. Each pair cannot both be zero. This chapter uses the given coordinates directly, without scaling, so we consistently use lowercase .
Next compute
, pronounced “delta,” names this difference of products. The topic on finding intersections by elimination already explains its role: when , the two lines have a unique intersection; when , 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 , call the unique intersection and use the formulas already derived:
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 . Check horizontal coordinates for a nonvertical input and vertical coordinates for a vertical input. Keep 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 is the only common point of those lines. If either segment excludes , no other candidate can exist elsewhere. Conversely, if 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 , but does not necessarily make . Whether the unique-intersection formula applies is always decided by the entire . Valid endpoint contacts are also retained because the range checks include both ends.
Zero denominator: Distinguish parallel lines from a shared line
When , stop using the unique-intersection formula. Substitute endpoint of the second input into the first line, and check whether
holds.
Why is one endpoint enough? The condition has already restricted the lines to being parallel or coincident. 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 is not on the first line, the lines cannot coincide. They are distinct parallel lines, so the segments' intersection is empty.
The assumption 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 . 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 therefore gives all three possibilities:
| Order along the common line | Complete intersection |
|---|---|
| is after | Empty |
| and are the same point | Point |
| is before | The complete closed interval of positive length from to |
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 . 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 , 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 first | Next check | Result |
|---|---|---|
| Both inputs are points | Are both coordinates respectively equal? | That point if equal; otherwise empty |
| Exactly one input is a point | Is it collinear with and within the closed range of the non-point input? | That point if both hold; otherwise empty |
| Both are non-points, | Is the unique candidate in both closed ranges? | The candidate if both hold; otherwise empty |
| Both are non-points, | Does an endpoint lie on the other supporting line? | Empty if not; otherwise continue to the collinear case |
| Both are non-points and collinear | Compare the selected | Empty, 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, 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 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 , 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, and . They may come from input endpoints or computed intersections. Chapter 2 processes events in increasing horizontal coordinate, so an exact comparison of decides which comes first: process first if , and first if . 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 from Section 3, and call the list position being checked . Being the same point requires both coordinates to be respectively equal:
The symbol 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 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 for the current event's horizontal coordinate. For the computed point , which has passed both range checks, calculate
is the candidate point's horizontal coordinate minus the current coordinate; the subscript identifies as the point being checked. Its sign directly determines registration:
| Exact result | Position relative to the current event | Register as a future crossing? |
|---|---|---|
| Strictly to the right | Add if not registered; otherwise retain the existing record | |
| Same horizontal coordinate as the current position | Do not add as a future crossing | |
| To the left, already passed | Do 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 ? Immediately after processing the crossing at , we have , so . 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 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 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 and the coefficients computed in Section 3. This section requires both segments to be nonvertical, so . Choose a horizontal coordinate covered by both segments, and write for the height of 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:
This is the result obtained there by rearranging and dividing by nonzero . Its new use here is to substitute the comparison position 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 :
The subscript reminds us of the subtraction order. A positive value means is above , 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 is , substituting gives . 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
where is the slope of the supporting line of . Comparing exactly checks the vertical order to the right of an ordinary crossing: if positive, 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 , 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 calculation | Sweep action and proof premise it supports |
|---|---|
| The intersection really belongs to both closed segments | Register only real crossings and do not wrongly exclude real intersections |
| Horizontal-coordinate order of distinct events | Process left events first, then use the updated active order |
| Both coordinates are respectively equal | Do not register a rediscovered crossing twice; record position separately from input-pair identity |
| The intersection's horizontal coordinate is strictly greater than the current coordinate | Newly 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 crossing | Maintain 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.