Hilbert Curve Calculator

Hilbert Curve Calculator

Emerging AI Skill PRO

Enter Calculation

Formula

g=s2n,4n points,4n−1 segmentsg = \frac{s}{2^n},\; 4^n \text{ points},\quad 4^n - 1 \text{ segments}
Usage stats

Advertisement

Results

Curve length L
127.5
units
Side s of the square 8
Order n 4
Segment length g 0.5
Number of segments 255
Number of points (cells) 256

Report an error on this page

What this calculator does

The Hilbert curve of order n is the path that visits the centres of all 4ⁿ small squares of a square of side s, cut into a 2ⁿ × 2ⁿ grid, moving each time to a neighbouring square. Order 1 is a U through the centres of the four quarters; each further order replaces every square by four smaller ones and links four copies of the previous order, turned so that they join end to end. As the order grows the path fills the square; its length doubles at each step.

Derivation of the formulas

Segment length:

$$ g = \frac{s}{2^n} $$

Number of points and segments:

$$ 4^n \text{ points},\quad 4^n - 1 \text{ segments} $$

Curve length:

$$ L = (4^n - 1)\,\frac{s}{2^n} = s\left(2^n - 2^{-n}\right) $$

The grid has 2ⁿ rows of 2ⁿ squares, so the curve visits 4ⁿ centres. Neighbouring centres are one small side s/2ⁿ apart, and the curve goes from each centre to a neighbouring one, so it consists of 4ⁿ − 1 segments of that length. Multiplying gives L = (4ⁿ − 1)s/2ⁿ = s(2ⁿ − 2⁻ⁿ), which nearly doubles with each order and grows without bound, although the curve stays inside the square.

A 16 × 16 grid of cell centers beside the same grid with a Hilbert path connecting all 256 centers.
The 16 × 16 grid gives the curve one point per square, at its center. The Hilbert path visits all 256 points exactly once, moving between squares that share an edge.

Solving from what you know

Choose what you know: Side s of the square and order n; Curve length L and order n; Segment length g and order n. The calculator first finds the sizes that define the curve and then computes every other value from the formulas above. Each length is accepted from 0.000001 to 1,000,000,000; anything outside is refused with a sentence that names the range. The order is a whole number from 1 to 15 (order 15 already has more than a billion points).

Advertisement

Worked example

With the defaults s = 8 and n = 4: the grid has 16 × 16 squares of side 0.5, the curve has 256 points and 255 segments, and its length is 255 × 0.5 = 127.5.

Results are accurate to at least 9 significant digits for ordinary inputs. When a result depends on the small difference of two nearly equal entries (closer than one part in a million), fewer digits are reliable. They are shown with up to 6 decimal places; a value below 0.000001 or of 10¹⁵ and more is shown as its plain 12-digit number instead. The unit you pick is a label: lengths are shown in that unit and areas in its square.

The example inputs lead to equal-length curve segments and their total length.
With side 8 and order 4, the 255 segments each have length 0.5, giving a total curve length of 127.5.

Frequently asked questions

Why does the length double with each order?

Each order has four times as many segments, each half as long, so the length roughly doubles: s(2ⁿ − 2⁻ⁿ). The limit curve passes through every point of the square and has no finite length.

Where does the curve start and end?

It starts in the centre of one corner square and ends in the centre of the neighbouring corner square on the same side, so the side length is the side of the whole square, not the distance between the ends.

How accurate are the results?

Results are accurate to at least 9 significant digits for ordinary inputs. When a result depends on the small difference of two nearly equal entries (closer than one part in a million), fewer digits are reliable.

Advertisement

References

Hans Sagan, Space-Filling Curves, Springer, 1994

Last updated:

Most popular in Geometry

View all Geometry calculators