[Tex/LaTex] Tikz Fractal – Uniform Cantor Set

lindenmayertikz-pgf

To go along with my other fractals: Tikz Fractal – Cantor Dust and Tikz Fractal – Menger Sponge, which you lovely people have helped my create, I would like to construct a "uniform Cantor set".

The construction is as follows:

Take the unit interval [0,1] and at each stage replace each interval with (a fixed number) n intervals of length less than |I|/n, where |I| is the length of the interval, and where an end point of the each of the subintervals coincides with the end point of its 'father' interval.

Here is a picture to try to make my shoddy explanation a little clearer:
enter image description here

The standard middle third Cantor set is where n=2 and |I|=1/3:
enter image description here

Short of working out all of the length and spacings, how can I construct this "automatically"? My thinking is that I should use a linedenmayer system but I have not done this for line segments before.

Best Answer

With lindenmayer system.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{cantor set}{
  \rule{F -> FfF}
  \rule{f -> fff}
}
\begin{document}
\begin{tikzpicture}
  \foreach \order in {0,...,4}
    \draw[yshift=-\order*10pt]  l-system[l-system={cantor set, axiom=F, order=\order, step=100pt/(3^\order)}];
\end{tikzpicture}
\end{document}

enter image description here

A Cantor set with a division into three bits is a trivial extension of the existing one:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{cantor set}{
  \rule{F -> FfFfF}
  \rule{f -> fffff}
}
\begin{document}
\begin{tikzpicture}
  \foreach \order in {0,...,4}
    \draw[yshift=-\order*10pt]  l-system[l-system={cantor set, axiom=F, order=\order, step=100pt/(5^\order)}];
\end{tikzpicture}
\end{document}

enter image description here

Related Question