Hilbert curve
Start with a U on a 2 × 2 grid. Press Next to lay out four copies, then join them into one continuous path.
Video walkthrough
Four copies make the next level
On a 2 × 2 grid, start at the top left, go down, go right, then go up. The path starts and ends in the top corners. That choice of endpoints makes it possible to reuse the same shape at the next level.
Divide the 4 × 4 grid into four quarters. Visit them in this order: top left, bottom left, bottom right, top right. Keep the two bottom copies in their original orientation. Reflect the upper-left copy across its main diagonal and the upper-right copy across its other diagonal. With those orientations, the exit of each copy shares a side with the entrance of the next.
Each copy visits its own cells exactly once. A reflection or translation preserves the distance between consecutive cells. The three links between the quarters each cross one cell edge, so the whole path still visits every cell with one-cell moves. Apply the same rule to go from 4 × 4 to 8 × 8.
| 1 | 2 | 15 | 16 |
| 4 | 3 | 14 | 13 |
| 5 | 8 | 9 | 12 |
| 6 | 7 | 10 | 11 |
Two ways to order the same pixels
Open Compare routes. It starts on visit 4 of a 4 × 4 grid. Press Next. In Hilbert order, the marker moves down one cell in the first column. In row-by-row order, it jumps from the far right of the first row to the far left of the next. Dashed lines show those jumps.
Think of storing the pixels in one list. If you highlight visits 1–4 on the 4 × 4 grid, row-by-row order picks the whole top row. Hilbert order picks the top-left 2 × 2 block. Keeping small patches together in a list is useful when indexing spatial data. Consecutive Hilbert visits share an edge. Neighboring cells can have far-apart indexes, as visits 2 and 15 show.
From a grid path to a space-filling curve
Level n visits 4ⁿ cell centers on a 2ⁿ × 2ⁿ grid and connects them with 4ⁿ − 1 segments. Each finite drawing is a polyline. As levels increase, the cells shrink and the continuous limiting curve reaches every point of the square.
The code below builds all the coordinates. For N = 4ⁿ cells, construction takes O(N) time and O(N) memory including the returned list. The recursive calls are nested n levels deep. The construction player stores each grid size, using the same list for the four separate copies and the joined 4 × 4 path. The comparison player shares its coordinate lists across frames.
JavaScript implementation
Coordinates start at zero. x increases to the right and y increases downward.
Show executable code
function hilbert(level) {
if (!Number.isInteger(level) || level < 1 || level > 4) {
throw new RangeError("Use a level from 1 to 4");
}
function build(side) {
if (side === 1) return [{ x: 0, y: 0 }];
const half = side / 2;
const part = build(half);
return [
...part.map(({ x, y }) => ({ x: y, y: x })),
...part.map(({ x, y }) => ({ x, y: y + half })),
...part.map(({ x, y }) => ({ x: x + half, y: y + half })),
...part.map(({ x, y }) => ({ x: side - 1 - y, y: half - 1 - x }))
];
}
return build(2 ** level);
}
console.log(hilbert(2));