Space-Filling Curves

Hilbert Curve

A continuous fractal curve that passes through every point of a unit square — a 1D line that fills 2D space.

The Construction

The Hilbert curve is built by recursion. At each level, the unit square is divided into four quadrants and the curve visits each exactly once, connecting them end-to-end:

Axiom: A
A → +BF−AFA−FB+
B → −AF+BFB+FA−

F draws a segment; + and are 90° turns; A and B control orientation.

First order: the simplest U-shaped path that visits all four quadrants once. Four segments, three turns.

Increasing Order

Each iteration replaces every unit square with a scaled-down copy of the whole construction. The number of segments quadruples at each step; the segment length halves.

Second order: 16 segments. The overall U-shape is still visible but each arm is now itself a smaller U. The curve begins to meander.

Space-Filling

At infinite order, the limiting curve passes through every point in the unit square exactly once. It is surjective (onto the square) but not injective — the limit curve is a continuous bijection from [0,1] to [0,1]².

Fourth order: 256 segments. The coverage is dense. The locality-preserving property is already useful at this resolution: nearby positions along the 1D curve are nearly always nearby in 2D space. This property underpins Hilbert-curve indexing in spatial databases and image dithering.