The Hilbert Curve
A cleaner space-filling curve than Peano's, and — unlike Peano's — one with real engineering applications.
History
A year after Peano’s shockwave, David Hilbert gave a cleaner, more geometric construction, published in 1891. Where Peano subdivides each cell into a 3×3 grid, Hilbert’s subdivides into 2×2 — a simpler picture that Hilbert, unlike Peano, actually drew.
Hilbert was, by the 1890s, already one of the most influential mathematicians alive — he would go on, in 1900, to pose the 23 problems that shaped twentieth-century mathematics. His space-filling curve paper is barely two pages long.
Construction
Like every curve in the L-Systems chapter, this one comes from an axiom and a pair of rules, rewritten generation after generation and handed to a turtle. Before looking at the rules themselves, it’s worth watching what the very first rewrite actually draws.
Applying rule just once to the axiom A gives the string -BF+AFA+FB-. The turtle only responds to F, +, and - — the As and Bs inside it are placeholders for what happens on the next rewrite, not drawing instructions — so walking this string means: turn right, forward, turn left, forward, turn left, forward, turn right. Three segments, a simple “U”:
The same two rules, four generations in. Each generation replaces every A and B with a slightly rotated, slightly reflected copy of the whole pattern — by generation 4, the individual U-shapes have already dissolved into the familiar dense curve.
Now the full rule set — the one every one of those generations actually used:
Each step multiplies the segment count by : . Unlike Peano’s curve, the Hilbert curve never revisits a point, and its quadrant ordering gives unusually strong locality: nearby points along the curve stay nearby in the plane.
Locality — not just space-filling — is what makes Hilbert’s curve special. Two points close together along its 1-D parameter are, with high probability, also close together in 2-D. This holds far better for Hilbert’s ordering than for Peano’s or a naive row-by-row scan.
Python implementation
Pseudocode first
hilbert(n) {
word = rewrite("A", HILBERT_RULES, n)
return turtle(word, angle=90)
}
The executable version keeps the same shape; shared plotting/export details live in fractalfair_helpers.py.
def hilbert(iterations=5):
word = rewrite(AXIOM, RULES, iterations)
return np.array(turtle_segments(word, 90))
rules = M.fromList
[ ('A', "-BF+AFA+FB-")
, ('B', "+AF-BFB-FA+") ]
main = mainWith (hilbertDiagram 5 # centerXY # frame 0.05)
Figure: 1023 segments visiting all 1024 cells of a 32×32 grid without ever crossing itself.
The Hilbert curve’s locality underlies real spatial-indexing systems (Uber’s H3, Google’s S2), improves cache-hit rates when traversing large 2-D/3-D arrays, and appears in colour-quantisation and IP-address visualisation tools.