how to produce a Sierpinski squared carpet with tikz? I saw many Sierpinski triangles on the web, but I cannot find anything with the square. Anyone knows how to do it?
Asked
Active
Viewed 1,015 times
2
-
because it's bunch of squares that can be placed with loops? – percusse Dec 01 '16 at 06:12
-
Maybe, but I cannot do that. – Francesco Dec 01 '16 at 06:26
-
9I think you might be a little more eager to contribute rather than just mentioning the name and waiting for an answer. You can at least put a picture of it – percusse Dec 01 '16 at 06:31
1 Answers
14
Might take ages...
\documentclass[tikz,border=5]{standalone}
\newcount\sierpinskiorder
\newcommand\sierpinskicarpet[2][]{%
\tikzset{sierpinski/.cd,#1}%
\sierpinskiorder=#2\relax%
\path [sierpinski/background/.try] (0,0) rectangle (1,1);
\SierpinskiCarpet}
\def\SierpinskiCarpet{{%
\ifnum\sierpinskiorder>0\relax
\path [sierpinski/foreground/.try] (1/3, 1/3) rectangle ++(1/3, 1/3);
\advance\sierpinskiorder by -1\relax
\foreach \x in {0,...,2}{\foreach \y in {0,...,2}{
\begin{scope}[shift={(\x/3,\y/3)},scale=1/3]
\SierpinskiCarpet
\end{scope}
}}
\fi
}}
\begin{document}
\begin{tikzpicture}
\sierpinskicarpet[foreground/.style={fill=white},
background/.style={top color=blue, bottom color=red}]{5}
\end{tikzpicture}
\end{document}
Mark Wibrow
- 70,437
