答案1
我只是改编了杰克的谢尔宾斯基三角:如何在 LaTeX 中创建谢尔宾斯基三角形?
输出
代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}%
\def\hexagwidth{2cm}%
\pgfdeclarelindenmayersystem{Sierpinski hexagon}{
\symbol{X}{\pgflsystemdrawforward}
\symbol{Y}{\pgflsystemmoveforward\pgflsystemmoveforward\pgflsystemmoveforward}
\rule{X -> X+X+X+X+X+X+Y}
\rule{Y -> YYY}
}%
\foreach \level in {1,...,4}{%
\tikzset{
l-system={step=\hexagwidth/3^\level, order=\level, angle=60}
}%
\begin{tikzpicture}
\fill (0,0) l-system [l-system={Sierpinski hexagon, axiom=X}] ;
\end{tikzpicture}
}%
\end{document}
答案2
只是为了好玩,我利用 MetaPost 重现了这个分形图形,这要归功于递归宏。
vardef Sierpinski(expr A, B, n) =
save P; pair P[]; P0 = A; P1 = B;
for i = 1 upto 5:
P[i+1] = P[i-1] rotatedaround (P[i], 120);
endfor;
if n = 0: fill P0 for i = 1 upto 5: -- P[i] endfor -- cycle;
else:
for i = 0 step 2 until 4:
Sierpinski(P[i], 1/3[P[i],P[i+1]], n-1);
Sierpinski(2/3[P[i],P[i+1]], P[i+1], n-1);
endfor; fi
enddef;
beginfig(1);
for n = 0 upto 4:
draw image(Sierpinski(origin, (2cm, 0), n)) shifted (n*4.5cm, 0);
endfor;
endfig;
end.