In this week's experiment I'll be diving into the Hilbert curve algorithm following along this video from The Coding Train.
What is the Hilbert Curve #
According to Wikipedia:
[...] a continuous fractal space-filling curve.
In simpler terms, it's a mathematical curve that can theoretically fill an entire two-dimensional area by following a precise recursive pattern.
Each iteration (called an order) increases the resolution, bending the line tighter and tighter until it nearly becomes solid.
Hilbert curves show up in computer science, image compression, and even dithering algorithms for grayscale rendering.
How It Works #
The sketch builds the curve point by point.
Each position in the sequence is converted into an (x, y) coordinate using a Hilbert index function: a mapping from a single integer to a two-dimensional location.
For every frame:
- The curve progresses one segment forward.
- Each segment's color is smoothly interpolated along a palette, creating a gradient through the path.
- Once the final point is reached, the animation halts, revealing the entire pattern.
The algorithm scales with order so higher orders produce increasingly dense, woven paths that almost resemble clothing fibers under a microscope.