Marching Squares is a computer graphics algorithm for extracting iso-contours (lines of constant value) from a 2D scalar field sampled on a regular grid. Each 2×2 square of grid cells has four corner values; thresholding each corner against an isovalue produces a 4-bit index (0–15), giving 16 possible configurations. A lookup table maps each configuration to one or two line segments that approximate where the contour crosses the cell, and linear interpolation on the edge gives sub-cell accuracy. The algorithm is the 2D precursor to the 3D Marching Cubes method (Lorensen & Cline, 1987) used in medical imaging to reconstruct surfaces from CT/MRI data.
The simulation lets you drag metaballs (potential field sources) across the canvas, sweep the isovalue threshold, and observe in real time how the 16-case lookup table generates smooth contour lines. Two saddle-cell cases (indices 5 and 10) are ambiguous; the simulator resolves them with bilinear interpolation of the centre value to choose the correct topology.
Why are there exactly 16 cases in the Marching Squares lookup table?
Each of the four corners of a cell is independently classified as above (1) or below (0) the isovalue, producing 2⁴ = 16 possible bit patterns. By symmetry many cases are rotations or reflections of each other, reducing the distinct geometries to about 4 unique shapes. The full 16-entry lookup table stores pre-defined edge crossing pairs for each pattern, enabling branch-free vectorised implementation.
What is the saddle-point ambiguity and how is it resolved?
Cases 5 (0101) and 10 (1010) have two diagonally opposite "inside" corners, creating ambiguity about whether the contour connects as an X-shape or two separate arcs. The standard resolution is to sample the scalar field at the cell centre: if the centre value exceeds the isovalue, the two inside corners are connected; otherwise they are separate. This bilinear disambiguation produces topologically consistent contours.
How does linear interpolation improve contour accuracy?
Without interpolation, contour segments would snap to cell edge midpoints, producing a staircase appearance. Linear interpolation places the crossing point at t = (iso − v₀) / (v₁ − v₀) along the edge between corner values v₀ and v₁, so the line segment accurately tracks where the scalar field equals the isovalue. For smooth fields this dramatically reduces visual aliasing with no additional sampling cost.
Marching Cubes is the 3D generalisation: each cubic cell has 8 corners, giving 2⁸ = 256 cases (reduced to 15 canonical by symmetry). It extracts triangulated iso-surfaces from volumetric data such as medical CT scans and simulated fluid fields. Introduced by Lorensen and Cline at SIGGRAPH 1987, it became one of the most-cited computer graphics papers, with medical imaging (MRI surface reconstruction) as its killer application.
Any 2D scalar field works: temperature from a weather model, pressure in a fluid simulation, distance to the nearest obstacle (for signed-distance-field rendering), electrostatic potential, terrain elevation, or the sum of radial basis functions (metaballs). The algorithm is field-agnostic and only needs values sampled on a regular grid.
Metaballs are point sources whose field contribution falls off with distance: f(x, y) = Σ rᵢ² / ((x−xᵢ)² + (y−yᵢ)²). Summing several metaballs creates a smooth blobby scalar field that merges and separates as sources move. Marching Squares extracts the iso-contour at a chosen threshold, producing the characteristic organic "blob" shapes popularised in 1980s computer graphics demos.
For a grid of M × N cells the algorithm processes each cell exactly once: O(MN) time and O(MN) space for the grid values. Contour extraction per cell is O(1) via lookup table. In practice, the bottleneck is often the scalar-field evaluation (e.g., summing k metaball contributions per cell costs O(kMN)), so GPU compute shaders are used to parallelise the field computation for real-time interactive applications.
Yes — it is commonly used for real-time fluid-surface rendering, fire simulation outlines, and interactive metaball editors. Since each cell is independent the algorithm parallelises perfectly on the GPU. Implementations typically run the field evaluation and contour extraction as compute/fragment shader passes, achieving thousands of frames per second on modern hardware for moderate grid sizes (e.g., 256×256).
Dual Contouring is an alternative that places contour vertices inside each cell rather than on edges, using both the scalar values and gradient information (normals) to position vertices. It better preserves sharp features (corners, ridges) that Marching Squares rounds off, at the cost of requiring gradient data and more complex topology tracking. It is preferred for mechanical CAD models and sharp-featured level-set surfaces.
This is an interactive implementation of the Marching Squares contouring algorithm, which extracts isolines — curves of constant value — from a 2D scalar field on a grid. Each cell’s four corners are compared against the isovalue to form a 4-bit index selecting one of 16 line configurations from a lookup table, with edges interpolated for smooth contours.
The canvas renders a live heatmap of the scalar field with isolines on top. Enabling cell indices labels each active cell with its 4-bit configuration and highlights the two ambiguous saddle cases in amber.
Choose a field type (Metaballs, Noise, Sinusoidal or Paint), then adjust Grid resolution and the Isovalue slider. Drag metaballs on the canvas, tick Multi-level for five isolines, Show heatmap or Show cell indices for diagnostics, and Resolve saddles to disambiguate cases 5 and 10.
Marching Squares is the direct 2D ancestor of Marching Cubes (Lorensen & Cline, 1987), used to reconstruct 3D surfaces from medical CT and MRI scans — one of computer graphics’ most-cited papers.
Each of a cell’s four corners is classified above or below the isovalue, giving 24 = 16 bit patterns. The lookup table stores the correct segments for every case, so geometry never needs computing on the fly.
Cases 5 and 10 have two diagonally opposite corners above the isovalue, so the contour could connect as an X or as two arcs. Resolve saddles samples the cell centre and picks whichever topology stays consistent with the surrounding field.
The isovalue is the threshold tested at every corner, so moving it slices the field at a different height. Near a metaball’s centre the field is high, so a high isovalue gives small tight loops, a low one large merged blobs.
It draws five isolines spaced around the current isovalue, like contour lines on a map, making the field’s overall shape and steepness easier to read than a single threshold.
Paint replaces the procedural field with one you sculpt by dragging on the canvas with a soft brush, showing that Marching Squares works on any scalar field, not just mathematical ones like metaballs.