1. Two segments
Every decision comes down to one segment ab of the first line against one segment cd of the second. The test asks which side of cd's line a and b are on (the sign of the area of triangle cda, and of cdb), and which side of ab's line c and d are on. If both pairs are on opposite sides, the segments cross strictly inside, at s = o₁ / (o₁ − o₂) of the way along ab.
The areas are computed in floating point with a bound on their rounding
error. A sign that is zero (a vertex exactly on the other line) or too
close to call goes to a careful path that decides it exactly, with
Shewchuk's orient predicate. Drag the points: they snap to
whole numbers, so you can put one exactly on the other line.
2. Paths and contacts
With more segments, one contact can be found by several pairs: a crossing
at a vertex shows up in both segments that meet there, and an overlap can
span many segments of each line. Contacts that touch along both lines are
joined into one piece, and each piece is one hit. An overlap's hit
is the point halfway along it, with its start and
end.
The type comes from where the first line arrives from and leaves to, relative to the second: opposite sides is a cross, the same side is a touch, and a hit where either path starts or ends is an end. Try the presets, then drag.
3. Which pairs to test
Lines of n and m segments have n×m
segment pairs, and nearly all of them are far apart. The search's job is
to find the few pairs worth testing. getIntersections looks
at the input and picks one approach, in this order:
- Boxes apart: if the lines' bounding boxes don't meet, there's nothing to do.
- A point: a line that stands still is checked against each segment of the other.
- Same samples: two series with the same x values need no search at all (3a).
- Merge: two lines that each only move left to right (or right to left) are walked side by side (3b).
- Grid: long segments crowded into a small shared area use a grid of segment-sized cells (3d); when that's 16 cells or fewer, every pair is tested directly.
- Tree: anything else gets a tree of bounding boxes over each line, and the two trees are descended together (3c).
Before any of that, a line with gaps (a missing coordinate) is split into
separate paths, and coordinates too large or small for safe arithmetic
are scaled by a power of two. Each figure below reports the approach that
getIntersections itself picks for its lines.
3a. Same samples
Two series sampled at the same x values share every interval, and on each one the gap yA − yB is a straight line. So the lines cross wherever that gap changes sign between neighbouring samples: one pass, no pair tests. The strip underneath shows which line is above at each sample.
3b. Merge
When both lines only move one way in x (series with their own x values, say), they can be walked together like merging two sorted lists. The first line is taken in runs of 4 segments. A cursor into the second line skips segments entirely left of the run, and the segments that start before the run ends are the only ones it can meet. Runs whose y ranges don't overlap are skipped. Step through the runs:
3c. Tree
For general lines, each line gets a binary tree of bounding boxes. The leaves are runs of 8 consecutive segments, and each level up joins two neighbours. Consecutive segments are already near each other, so the tree is built bottom-up in one pass, without sorting. The two trees are descended together from the roots: a pair of boxes that don't overlap is dropped with everything under it, and only leaf pairs that survive are tested segment by segment. Move from the roots down to the leaves; faint boxes have been ruled out.
3d. Grid
Scribbles whose segments are long compared with the space they fill defeat the tree: nearly every box overlaps nearly every other. Then a uniform grid of cells about one segment wide covers the area the two lines share. Each segment of the second line is listed in the cells its box touches (darker cells hold more), and each segment of the first line checks only the segments listed in its own cells. A pair that shares several cells is tested in just one of them. Pick a segment of the first line to see what it checks:
4. Every test case
Each card draws one case from cases.js and runs
getIntersections on it, both ways round, with the same check
as test.js. See expected-behavior.md for why
each case expects what it does.