Sierpinski triangle

The width of the background triangles is 2^order*stepsize, so you can calculate the step size as a function of the desired overall width:

Sierpinski triangle

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\trianglewidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
    \symbol{X}{\pgflsystemdrawforward}
    \symbol{Y}{\pgflsystemdrawforward}
    \rule{X -> X-Y+X+Y-X}
    \rule{Y -> YY}
}%
\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}
}%
\end{document}

Leave a Reply

Your email address will not be published.