# TikZ: Hilbert curves

I want to draw the first orders of a Hilbert curve and a Lebesgue curve. In order to draw a Hilbert curve I use the following tikZ code:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems}
\begin{document}

\pgfdeclarelindenmayersystem{Hilbert curve}{
\rule{L -> +RF-LFL-FR+}
\rule{R -> -LF+RFR+FL-}}

\begin{tabular}{cc}
\begin{tikzpicture}
\shadedraw [bottom color=white, top color=white, draw=black]
[l-system={Hilbert curve, axiom=L, order=4, step=8pt, angle=90}]
lindenmayer system;
\end{tikzpicture}
\end{tabular}
\end{document}


Then I tried to arrange the first orders side by side and I got this:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems}
\begin{document}

\pgfdeclarelindenmayersystem{Hilbert curve}{
\rule{L -> +RF-LFL-FR+}
\rule{R -> -LF+RFR+FL-}}

\begin{tabular}{cc}
\begin{tikzpicture}
\foreach \i in {1,...,4}{
\begin{scope}[xshift=2*\i cm,yshift=0cm,rotate=0]
\shadedraw [bottom color=white, top color=white, draw=black]
[l-system={Hilbert curve, axiom=L, order=\i, step=8pt, angle=90}]
lindenmayer system;
\end{scope}
}
\end{tikzpicture}
\end{tabular}
\end{document}


But they are not really good arranged. How can I arrange them in such a way, that they have the same size with a constant distance to each other?

I would appreciate any help!

This seems to do the trick : scale each by its size, computed in terms of each \i

By math, your drawings have size :

value of \i      relative size       formula
1                  1           2^1 - 1
2                  3           2^2 - 1
3                  7           2^3 - 1
4                 15           2^4 - 1


\documentclass[border=2pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems}
\begin{document}

\pgfdeclarelindenmayersystem{Hilbert curve}{
\rule{L -> +RF-LFL-FR+}
\rule{R -> -LF+RFR+FL-}}

\begin{tikzpicture}[scale=10]
\foreach \i in {1,...,4}{
\begin{scope}[xshift=.5*\i cm,yshift=0cm,rotate=0, scale = 1/(2^(\i)-1)]
\shadedraw [bottom color=white, top color=white, draw=black]
[l-system={Hilbert curve, axiom=L, order=\i, step=8pt, angle=90}]
lindenmayer system;
\end{scope}
}
\end{tikzpicture}
\end{document}


Cheers,

• Very clever. I like that solution! – Jan Jan 11 '17 at 19:54
• I couldn't live on math ;-) – Jan Jan 11 '17 at 20:08