A contour-tracing algorithm that extracts iso-lines from a 2D scalar field by classifying each grid cell into one of 16 cases and looking up the corresponding edge segments.
A scalar field assigns a numeric value to every grid vertex. Choose a threshold and each vertex is either inside (above threshold — bright) or outside (below — dark). A cell has four corners so there are 2⁴ = 16 possible configurations, indexed by a 4-bit number: TL = bit 3 (8), TR = bit 2 (4), BR = bit 1 (2), BL = bit 0 (1).
Each case maps to zero, one, or two line segments. Cases 5 and 10 are saddle ambiguities — the correct topology is genuinely ambiguous and either of the two valid interpretations can be chosen.
Click any corner to toggle it inside or outside the threshold. The case index, bit pattern, and contour segments update instantly.
Click a corner to toggle it.
A 26 × 14 grid of values computed from a sum of Gaussian blobs. Each vertex is classified by the threshold and marching squares runs on every cell. Drag the threshold to watch the contour move through the field.
Each contour vertex is placed by linear interpolation along the edge — not simply at the midpoint. The crossing position is t = (threshold − v₀) / (v₁ − v₀), producing smooth contours even on coarse grids.
The simplest implementation places each contour vertex at the exact midpoint of the crossing edge, ignoring the actual field values. Linear interpolation uses t = (threshold − v₀) / (v₁ − v₀) to find exactly where the field crosses. On a fine grid the difference is small; on a coarse grid the midpoint contour looks like a staircase of 45° steps.
The complete algorithm in three pieces. The surprising thing is how little code it takes — the domain knowledge lives entirely in the lookup table.
t = (threshold − v₀) / (v₁ − v₀) — positions the vertex precisely where the field crosses the threshold, assuming the field varies linearly between vertices. On a fine grid the difference is small; on a coarse grid (like the demo above) it prevents the contour from looking like a staircase of 45° segments.v₀ === v₁ the denominator is zero (the edge runs exactly along the iso-surface). The result is clamped to t = 0.5.