10

Is anyone able to help me draw the following fractal in Tikz? enter image description here

Here we start of with the unit square, break it into 16 equal squares, and remove 12 of them - leaving 4 arranged as seen. The process is repeated on the remaining squares, and so on.

I am pretty sure I need to use something similar to Nesting fractal tikz decorations but I am not good enough at TeX to compile the code myself.

Thank you in advance.

My code so far:

 \newcommand{\dust}[1]{
     \foreach \i in {1,...,#1}{decorate\{}
     (0,0)--(1,1)
     \foreach \i in {1,...,#1}{\}};

      SOMETHING IN HERE (DON'T KNOW)
 }

 \begin{tikzpicture}
     \path (0,0) pic {dust=5};
 \end{tikzpicture}
  • What have you tried so far? Please show us your code of what you have attempted. – Huang_d Jul 10 '17 at 16:16
  • Welcome to TeX.SX! Please expand your code to a minimal working example (MWE). Reproducing will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Jul 10 '17 at 21:00
  • Instead of decorations, you could use a normal tikzpicture as in tex.stackexchange.com/questions/228106/fractals-in-tikz?rq=1 – John Kormylo Jul 10 '17 at 21:00
11

It is a nice Lindenmayer system exercise.

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\pgfdeclarelindenmayersystem{Cantor dust}{
    \symbol{S}{\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\pgflsystemcurrentstep}{\pgflsystemcurrentstep}}}
    \symbol{U}{\pgftransformyshift{\pgflsystemcurrentstep}}
    \symbol{R}{\pgftransformxshift{\pgflsystemcurrentstep}}
    \rule{S -> [UUSURRS][RSRRUS]}
    \rule{U -> UUUU}
    \rule{R -> RRRR}
}
\tikz;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step=320pt,order=1}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step= 80pt,order=2}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step= 20pt,order=3}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step=  5pt,order=4}]lindenmayer system;
\end{document}

  • This is exactly what I wanted :) thank you very much! – JSharpee Jul 11 '17 at 13:40
  • @Symbol I am trying to adapt this code to make the rules change the image to 5 squares of equal size instead of 4. I want 4 of the squares to be in the corners and 1 in the center. Could you help me please? – PercyF2519 Jul 17 '17 at 13:15
  • @PercyF2519 The rules should be \rule{S -> S[U[US][RS]][RRSUUS]}\rule{U -> UUU}\rule{R -> RRR}. – Symbol 1 Jul 17 '17 at 15:17
  • Thank you, I did manage to work out the code but mine isn't quite and neat as yours. Thanks again. – PercyF2519 Jul 19 '17 at 12:28
5

I've just discovered this topic, which was related to another, more recent one. It can be considered as an application of recursive programming, for which MetaPost is usually well suited. So here is a MetaPost solution, for whom it may interest.

def cantor_dust(expr x, y, d, n) = 
    if n > 0:
        cantor_dust(x+.25d, y, .25d, n-1);
        cantor_dust(x+.75d, y+.25d, .25d, n-1);
        cantor_dust(x+.5d, y+.75d, .25d, n-1);
        cantor_dust(x, y+.5d, .25d,  n-1);
    else: fill (x, y) -- (x+d, y) -- (x+d, y+d) -- (x, y+d) -- cycle; fi
enddef;
beginfig(1);
    for i = 0 upto 4:
        draw image(cantor_dust(0, 0, 4cm, i)) shifted (4.25cm*i, 0);
    endfor;
endfig;
end.

enter image description here

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.