3

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.

5

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

  • 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
  • 1
    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
3

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}

enter image description here

3

A solution based on boxes and rules:

\documentclass{article}

\makeatletter
\newcommand*{\gen}[1]{%
  \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}[2]{%
  \begin{minipage}{#1}%
    \gen{#2}%
  \end{minipage}%
}
\makeatother

\begin{document}
  \noindent
  \genpic{\linewidth}{8}
\end{document}

Result

  • Thank you very much ! This is obviously much more idiomatic than my approach :D – marsupilam Aug 13 '17 at 22:11
1

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.

enter image description here

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.