Marching-Squares Contour Extraction

Extract iso-contours from a 2D scalar field on a raster by classifying each 2×2 cell's corners against a threshold, looking up which cell edges the contour crosses, and locating each crossing by linear interpolation.

Step 1 of 157%

Tutorial

From Scalar Field to Case Index

In live air-routing, hazard fields such as convective intensity, icing severity, or turbulence index arrive as scalar values sampled on a regular grid. To plan around hazards, we need the iso-contour of the field at a chosen threshold τ\tau — the curve along which the field equals τ\tau. Marching squares is the standard per-cell algorithm: it walks every 2×22 \times 2 cell of the grid and emits the contour segments inside that cell.

For each cell, label the four corners 0033 counterclockwise starting from the bottom-left:

corner 3 (TL)corner 2 (TR)corner 0 (BL)corner 1 (BR)\begin{array}{cc} \text{corner }3\text{ (TL)} & \text{corner }2\text{ (TR)} \\ \text{corner }0\text{ (BL)} & \text{corner }1\text{ (BR)} \end{array}

Compare each corner value to τ\tau. A corner is inside if its value is τ\geq \tau and outside otherwise. Packing the four inside/outside bits into a single integer gives the case index:

c=b01+b12+b24+b38,c = b_0 \cdot 1 + b_1 \cdot 2 + b_2 \cdot 4 + b_3 \cdot 8,

where bi=1b_i = 1 if corner ii is inside and bi=0b_i = 0 otherwise. The case index takes integer values from 00 to 1515 and tells us which contour-segment pattern the cell emits.

For example, a cell with corner values 1.2,4.7,5.0,0.81.2,\,4.7,\,5.0,\,0.8 (in BL, BR, TR, TL order) and threshold τ=3.0\tau = 3.0 has b0=0b_0=0, b1=1b_1=1, b2=1b_2=1, b3=0b_3=0, giving

c=0+2+4+0=6.c = 0 + 2 + 4 + 0 = 6.
navigate · Enter open · Esc close · ⌘K/Ctrl K toggle