# Tikz Fractal - Uniform Cantor Set

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: The standard middle third Cantor set is where n=2 and |I|=1/3: 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.

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} 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} • That looks really good, thank you. Could you briefly explain your code? (how do I make it into 3 equally spaced sections?) – JSharpee Aug 13 '17 at 21:52
• So the F are drawn, due to axiom=F, and all others words of the grammar (here f) take up blank space ? – marsupilam Aug 13 '17 at 21:52
• I think this is the droid I am looking for, thanks. I will have a play with it. – JSharpee Aug 13 '17 at 21:56
• Ah sorry, just looked at the doc : F and f are library keywords for draw a line and take up space. The axiom=F option sets the initial state, the root from which next steps are iteratively expanded following the system's rules. (By the way, very elegant code ! It would be a nice example for the TikZ manual.) – marsupilam Aug 13 '17 at 22:04
• Is there any way to get it so the gaps in between are not the same length as the lngths of the intervals? – JSharpee Aug 13 '17 at 22:08

You can use the Cantor set decoration from the decorations.fractals library. An example is given in the manual:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}
\begin{document}
\begin{tikzpicture}[decoration=Cantor set,very thick]
\draw decorate{ (0,0) -- (3,0) };
\draw decorate{ decorate{ (0,-.5) -- (3,-.5) }};
\draw decorate{ decorate{ decorate{ (0,-1) -- (3,-1) }}};
\draw decorate{ decorate{ decorate{ decorate{ (0,-1.5) -- (3,-1.5) }}}};
\end{tikzpicture}
\end{document} A solution based on boxes and rules:

\documentclass{article}

\makeatletter
\newcommand*{\gen}{%
\hrule height 5mm\relax
\ifnum#1>0 %
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{%
\kern5mm\relax
\hbox to \hsize{%
\vbox{%
\hsize=.3333\hsize
\gen{\numexpr#1-1}%
}\hfill
\vbox{%
\hsize=.3333\hsize
\gen{\numexpr#1-1}%
}%
}%
}%
}
\newcommand*{\genpic}{%
\begin{minipage}{#1}%
\gen{#2}%
\end{minipage}%
}
\makeatother

\begin{document}
\noindent
\genpic{\linewidth}{8}
\end{document} • Thank you very much ! This is obviously much more idiomatic than my approach :D – marsupilam Aug 13 '17 at 22:11

I know nothing about Lindenmayer systems, but this is easy to do using TeX recursivity.

## The output

the PNG below doesn't look great but the PDF is alright.

The code starts getting slow from n=11 or so on. ## The code

\documentclass[12pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[xscale=20,yscale=2]
\def\gen|#1|
{
\if 0#1
\path[fill] (0,0) rectangle (1,.8) ;
\else
\begin{scope}[xscale=1/3,yshift=-1cm]
\pgfmathtruncatemacro{\k}{#1-1}
\gen|\k|
\begin{scope}[xshift=2cm]
\gen|\k|
\end{scope}
\end{scope}
\fi
}

\foreach \k in {0,...,11}
{
\gen|\k|
}

\end{tikzpicture}
\end{document}