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.
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]); }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
threshold: number — Iso-value at which the contour is extracted from the scalar field.resolution: number — Grid cell size; smaller cells give smoother contours.interpolate: boolean — Whether edge crossings are linearly interpolated for smoother lines.
Lineage
Frequently combined with
- Perlin / Simplex Noise · 13 works
- HSL / Hue Cycle · 10 works
- Bezier / Catmull-Rom Splines · 7 works
- Fragment Shader (GLSL) · 5 works
- Flow Field / Vector Field · 4 works
- Delaunay Triangulation · 3 works
- Particle System · 3 works
- Quadtree / Recursive Quad Split · 2 works
- Voronoi Diagram · 2 works
- Audio-Reactive · 2 works
- Poisson Disk Sampling · 1 work
- 3D Mesh / Geometry · 1 work
Works using this algorithm (26)
Last updated 2026-07-16