6

I'd like to adapt the example here to create a triangle similar to the Sierpinski gasket: How to create a Sierpinski triangle in LaTeX?

Essentially I'd like to create the altered form of the gasket pictured below. Even just the first iteration of the fractal (with 3 triangles removed from the bigger triangle) would be great! However, I haven't been able to work out how to adapt the techniques from the link above. Any help would be greatly appreciated!

Pascal's Triangle Mod 3

Black Mild
  • 17,569

1 Answers1

11

Welcome! This is a rather close reproduction of your screen shot with the lindenmayersystems library.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\trianglewidth{6cm}%

\pgfdeclarelindenmayersystem{Sierpinski triangle 3}{%
    \symbol{X}{\pgflsystemdrawforward}%
    \rule{X -> X-X+X+X-X-XX+X+X-X+X-X}%
}%
\pgfmathtruncatemacro{\level}{4}%
\tikzset{%
    l-system={step={\trianglewidth/pow(3,\level)}, order=\level, angle=-120}
}%
\begin{tikzpicture}
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\clip (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
    \draw [draw=none] (0,0) l-system
    [l-system={Sierpinski triangle 3,
    axiom=X},fill=white,draw,line width=1pt/\level,line join=round];
\end{tikzpicture}
\end{document}

enter image description here

Let's now discuss how one may get there. First of all,I think the post you link to can be simplified. As far as I can see, there is no need for the Y rule. This observation also allows one to generalize the construction.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems,backgrounds}

\begin{document}%
\def\trianglewidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
    \symbol{X}{\pgflsystemdrawforward}
    \rule{X -> X-X+X+X-X}
}%
\foreach \level in {0,...,3}{%
\tikzset{
    l-system={step=\trianglewidth/(2^\level), order=\level, angle=-120}
}%
\begin{tikzpicture}
    \fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
    \draw [draw=none] (0,0) l-system
    [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{tikzpicture}
}%

\pgfdeclarelindenmayersystem{Sierpinski triangle 3}{
    \symbol{X}{\pgflsystemdrawforward}
    \rule{X -> X-X+X+X-X-XX+X+X-X+X-X}
}%
\foreach \level in {1,...,4}{%
\tikzset{
    l-system={step={\trianglewidth/pow(3,\level)}, order=\level, angle=-120}
}%
\begin{tikzpicture}[background rectangle/.style={fill=blue},
show background rectangle]
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
    \draw [draw=none] (0,0) l-system
    [l-system={Sierpinski triangle 3, axiom=X},fill=white];
\end{tikzpicture}}
\end{document}

The regular Sierpinski triangles get reproduced:

enter image description here

The new ones are drawn here with a background to show that there is no cheating going on, i.e. no unnecessary stuff gets drawn in white.

enter image description here

And here is a "new only" background free version.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems}

\begin{document}%
\def\trianglewidth{3cm}%

\pgfdeclarelindenmayersystem{Sierpinski triangle 3}{
    \symbol{X}{\pgflsystemdrawforward}
    \rule{X -> X-X+X+X-X-XX+X+X-X+X-X}
}%
\foreach \level in {1,...,3}{%
\tikzset{
    l-system={step={\trianglewidth/pow(3,\level)}, order=\level, angle=-120}
}%
\begin{tikzpicture}
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
    \draw [draw=none] (0,0) l-system
    [l-system={Sierpinski triangle 3, axiom=X},fill=white];
\end{tikzpicture}}
\end{document}

enter image description here

To get something that resembles your screen shot more, we can add a line width.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\usetikzlibrary{lindenmayersystems}

\begin{document}%
\def\trianglewidth{3cm}%

\pgfdeclarelindenmayersystem{Sierpinski triangle 3}{
    \symbol{X}{\pgflsystemdrawforward}
    \rule{X -> X-X+X+X-X-XX+X+X-X+X-X}
}%
\foreach \level in {1,...,3}{%
\tikzset{
    l-system={step={\trianglewidth/pow(3,\level)}, order=\level, angle=-120}
}%
\ifnum\level>1
~
\fi
\begin{tikzpicture}
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\clip (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
    \draw [draw=none] (0,0) l-system
    [l-system={Sierpinski triangle 3,
    axiom=X},fill=white,draw,line width=1pt/\level,line join=round];
\end{tikzpicture}}
\end{document}

enter image description here

If you are curious what's going on here: just imagine you are a turtle and crawl along some path. Whenever there is an X, this means you crawl some distance, if there is a +, you turn by -120 (why -120? because Jake said so and it is not a good idea to argue with an owl;-) and if there is a - you turn by 120 degrees. There is another library that allows us to illustrate this (and one can just search and replace the X, + and - by the corresponding turtle keys).

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{turtle}
\newcounter{iturt}
\begin{document}
\begin{tikzpicture}[scale=2,
pics/arrow/.style={code={\draw[-latex] (-0.5,0) -- (0,0);}}]
\draw [turtle={how/.style={to path={-- (\tikztotarget)
node[pos=0.7,auto]{\stepcounter{iturt}\number\value{iturt}}
pic[pos=1,sloped,allow upside down]{arrow}}},
home,right=90,forward,right=-120,forward,left=-120,forward,left=-120,forward,right=-120,forward,right=-120,forward,forward,left=-120,forward,left=-120,forward,right=-120,forward,left=-120,forward,right=-120,forward}];
\end{tikzpicture}
\end{document}

enter image description here

One can also use the parser module, maybe this is even simpler, but the bottom-line is that TikZ has tools that allow one to visualize what's going on here.

  • One can introduce an Y rule to avoid that the same triangles get filled too often. \pgfdeclarelindenmayersystem{Sierpinski triangle 3}{% \symbol{X}{\pgflsystemdrawforward}% \symbol{Y}{\pgflsystemdrawforward}% \rule{X -> Y-X+Y+X-Y-XX+X+Y-X+Y-X}% \rule{Y -> YYY}% }% Ironically, the compilation time for this one is slightly larger than for the rule set presented at the beginning of the answer. Naively I'd think this should not happen, but it does seem to happen. –  Apr 08 '20 at 05:40