Hilbert Curve

A while ago the plan was to have at least 12 postscript based fractals so I could make some type of calendar; that is decorate each month with a different fractal image. Now I’m not sure what to think about the idea, a bit of a waste of time during covid, or an attempt at stretching one’s brain and trying to have fun at the same time.

The Hilbert curve is one of the more famous space filling curves out there. As per usual, I mostly care about the implementation and leave the analytical details to the various sites out there. Maybe one day I might try to implement the 3D version; surprisingly I stumbled upon this, so it might be ok.

The definition of the Hilbert curve is self-similar and recursive, that is higher iterations depend on previous iterations that have been transformed (using flips and rotations). The following video has a great explanation about Hilbert curves.

With the implementation, the initial shape is a ‘cup’ type of shape where 4 points are connected to their closest neighbouring points using 3 edges/lines.

\Huge \sqcup\ \textsf{\small or}\ \sqsubset\ \textsf{\small or}\ \sqcap\ \textsf{\small or}\ \sqsupset

Next, each section of the curve is replaced using the following set of rules (it might look a bit daunting but its not too bad):

\Huge \begin{aligned} \sqcup &\Rightarrow\ \sqsupset\ \downarrow \sqcup \rightarrow \sqcup \uparrow\ \sqsubset \\ \sqsupset &\Rightarrow \sqcup \rightarrow\ \sqsupset\ \downarrow\ \sqsupset\ \leftarrow \sqcap \\ \sqcap &\Rightarrow\ \sqsubset\ \uparrow \sqcap \leftarrow \sqcap \downarrow\ \sqsupset \\ \sqsubset &\Rightarrow \sqcap \leftarrow\ \sqsubset\ \uparrow\ \sqsubset\ \rightarrow \sqcup \end{aligned}

Where, a particular type of shape is mapped to 4 other shapes (including itself) and the arrows correspond to the actual drawing of necessary line segments. Note that each mapping iteration requires that the line segment length to be reduced using the following formula; where \ell_0 is the original length, i is the iteration number, and \ell_i is the length at a specific iteration.

\ell_i = \frac{\ell_0}{2^i – 1}

Postscript code was written to generate the Hilbert curve at an arbitrary length and is available here. Below are two samples of the resulting output; on the left is an example with 7 iterations, while on the right show the sequence of the first 6 iterations.

Note, for a more creative touch, the line width can be adjusted. In the example on the right, the line width was chosen to be the same as the resulting white space.

Other Links

Since the Hilbert curve is fairly famous, it has been implemented by many others over the years. Here are some of my favourites:


No Comments

Add your comment