A line that fills a square

Hilbert curve

The Hilbert curve is the limit of a sequence of paths through a square: each visits every cell of a finer grid in turn; at each step, four smaller copies of the previous path are turned into position and joined end to end. The limit is a continuous curve that passes through every point of the square. Turn the order, move the probe, or light the square up.

The Hilbert curve in this visualization

In 1890 Giuseppe Peano gave a curve that passes through every point of a square, built on thirds; a year later David Hilbert gave a geometric construction built on halves. Order 1 is a U through the four cells of a 2×22 \times 2 grid. At each step, four smaller copies of the previous path are turned into position and joined end to end. Order nn visits all 4n4^n cells of a 2n×2n2^n \times 2^n grid, one step at a time, and its line is about 2n2^n long.

The orders are nested: the parameter is cut into equal pieces, one per cell, and the cells for each piece are the sub-cells of the cell for the piece containing it in the order before. A fixed tt lies in nested pieces, whose cells shrink to one point H(t)H(t); neighbouring pieces get neighbouring cells, so HH is continuous. Conversely, take any point of the square and pick, order after order, nested small cells containing it. Their closed parameter pieces are nested too and share some tt; the cells shrink to the point, so H(t)H(t) is that point. No finite order does this: each joins the centres of its cells by straight segments.

Points close on the line stay close in the square: a parameter interval of width ℓ\ell stays within a few times ℓ\sqrt{\ell} of its start, all in one piece, as the probe’s window shows. The other way round fails: two cells side by side in the square can sit far apart on the line. It has to fail. A continuous curve that visited every point exactly once would make the square and a line segment the same shape, and they are not: remove an interior point from a segment and it falls into two pieces, remove one from a square and it does not. So the limit curve passes through some points more than once.

The Z order visits the cells in the same quarters but simply interleaves the binary digits of their coordinates. It is easy to compute and widely used for indexing, but it jumps: halfway along, one step goes from the rightmost column to the leftmost. Some map formats number tiles in Hilbert order, so consecutive tile IDs at one zoom level follow neighbouring tiles.

Things to notice

  • Every order you can draw misses almost every point of the square; only the limit curve fills it.
  • The limit is still a curve, a continuous image of a line segment, yet it covers an area: the parameter is one-dimensional, the image is the whole square.

Related