2

I am trying to create two copies of the Sierpinski triangle like this: enter image description here

For this I borrowed some code I found online and tried to modify it a little bit to meet my needs. Here's my edited version of the code:

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

\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}
}%

\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}

\begin{tikzpicture}[scale=2]
\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}

However, I don't know how to create the second copy of the Sierpinski triangle in the same tikzpicture enviroment (minipage does not work for my purpose). I tried to create a copy of the code inside the tikzpicture environment and then modify some parameters but it didn't work. This is the code of that attempt:

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

\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}
}%

\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}

\begin{tikzpicture}[scale=2]
\fill [black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (0,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];

% It does not look like the Sierpinski triangle
\fill [black] (3,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
\draw [draw=none] (3,0) l-system [l-system={Sierpinski triangle, axiom=X},fill=white];
\end{tikzpicture}
\end{document}

So my question would be: how can I modify the code to obtain the two copies of the Sierpinki triangles?

Note: The code was taken from here and was created by Jake on TeX SE.

1

1 Answer 1

2

use scope environement

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

\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}
}%

\tikzset
{
l-system={step=\trianglewidth/(2^6), order=6, angle=-120}
}

\begin{tikzpicture}[scale=2]
\begin{scope}
\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{scope}
\begin{scope}[shift={(5cm,0)}]
\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{scope}
\end{tikzpicture}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .