Marching Squares / Contours

First appeared 1987 · William E. Lorensen, Harvey Cline — 2D case of Marching Cubes — Lorensen & Cline (1987). · timeline ↗

community / folk Name — Descriptive: the 2D analogue of Marching Cubes (Lorensen & Cline, 1987); the name follows that of its 3D sibling rather than a separate coinage.

In one line A 2D method for extracting contour lines (isolines) from a scalar field — the 2D case of Marching Cubes. Used for contour maps, metaball outlines and iso-surfaces of any value field.

Marching Squares / Contours · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

marching-squares/index.html
const SEED = Math.floor(Math.random()*1e9);

function setup() {
  noiseSeed(SEED); randomSeed(SEED);
  const s = Math.min(windowWidth, windowHeight);
  createCanvas(s, s);
  background('#0a0a0a');
  strokeCap(ROUND);

  // Sample a noise field on a coarse grid, then run marching-squares
  // to extract iso-lines for several thresholds.
  const G = 60;                // grid resolution (G x G cells)
  const cs = s / G;            // cell size
  const FREQ = 0.04;
  const ISOS = [0.4, 0.5, 0.6, 0.7];
  const COLORS = ['#264653', '#2a9d8f', '#e9c46a', '#e76f51'];

  // Build the scalar field.
  const f = new Array((G + 1) * (G + 1));
  for (let y = 0; y <= G; y++) {
    for (let x = 0; x <= G; x++) {
      f[y * (G + 1) + x] = noise(x * FREQ, y * FREQ);
    }
  }

  strokeWeight(s * 0.0025);
  for (let i = 0; i < ISOS.length; i++) {
    stroke(COLORS[i]);
    const iso = ISOS[i];
    for (let y = 0; y < G; y++) {
      for (let x = 0; x < G; x++) {
        const tl = f[y * (G + 1) + x];
        const tr = f[y * (G + 1) + x + 1];
        const br = f[(y + 1) * (G + 1) + x + 1];
        const bl = f[(y + 1) * (G + 1) + x];
        // 4-bit corner classification
        let c = 0;
        if (tl > iso) c |= 8;
        if (tr > iso) c |= 4;
        if (br > iso) c |= 2;
        if (bl > iso) c |= 1;
        if (c === 0 || c === 15) continue;
        // edge midpoints (linearly interpolated)
        const px = x * cs, py = y * cs;
        const T = [px + lerpEdge(tl, tr) * cs, py];
        const R = [px + cs, py + lerpEdge(tr, br) * cs];
        const B = [px + lerpEdge(bl, br) * cs, py + cs];
        const L = [px, py + lerpEdge(tl, bl) * cs];
        function lerpEdge(a, b) { return (iso - a) / (b - a); }
        // segment table for each case
        switch (c) {
          case 1: case 14: seg(L, B); break;
          case 2: case 13: seg(B, R); break;
          case 3: case 12: seg(L, R); break;
          case 4: case 11: seg(T, R); break;
          case 5: seg(L, T); seg(B, R); break;
          case 6: case 9: seg(T, B); break;
          case 7: case 8: seg(L, T); break;
          case 10: seg(T, R); seg(L, B); break;
        }
      }
    }
  }
}
function seg(a, b) { line(a[0], a[1], b[0], b[1]); }

Open the demo source ↗

How it works

Sample the scalar field on a grid and pick a threshold (iso-value). For each grid cell of four corners: 1. Classify each corner as inside (≥ threshold) or outside, giving a 4-bit index (16 possible cases). 2. A lookup table maps that index to which cell edges the contour crosses and how to connect them (the two ambiguous saddle cases are resolved by the centre value). 3. Place each crossing point along its edge by linear interpolation between the two corner values, so the contour follows the field smoothly. Joining the segments across all cells yields closed iso-curves; raising/lowering the threshold moves the contour.

Parameters

Lineage

Related
Metaballs, Reaction-Diffusion

Frequently combined with

Works using this algorithm (26)

Alien DNA
shvembldr
Ancient Courses of Fictional Rivers
robert-hodgin
Anticyclone
william-mapan
Archetype
kjetil-golid
Avalon
r0zk0
Cells
hevey
Cytographia
golan-levin
Diggly Collects All
dca
entretiempos
marcelo-soria-rodriguez
Fake Internet Money
steve-pikelny
Flows
ryley-o-eth
Glasshouse INAT
aleksandra-jovanic
Low Tide
artem-verkhovskiy-x-andy-shaw
Marfa Yucca
daniel-calderon-arenas
Metaballs
stanko-tadic
Neural Sediments
eko33
Para Bellum
matty-mariansky
Perpetua
punch-card-collective
Pizza 1o1
kala
Recursion
hevey
send/receive
snowfro
Space Birds
mark-cotton
Strata
vebj-rn-isaksen
Supermental
rosenlykke
The Opera
luke-shannon
Túnel Dimensional
autonomoses

Last updated 2026-07-16