Polyline intersections

getIntersections(a, b) finds where two polylines meet. Each polyline is a path from its first point to its last; every connected piece of contact is one hit, labelled a cross, a touch or an end. This page builds up how it works, from one pair of segments to the searches that keep large inputs fast, and then draws every test case.

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.

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.

  • first line (A)
  • second line (B)
  • cross
  • touch
  • end
  • overlap stretch
  • expected, not found