# Tikz Fractal - Sierpinski Hexagon

Yet another attempt at a Fractal construction. This time I am trying to draw the Sierpinski Hexagon:

I think this should be easy enough to do using l-system. My question is a slightly broader one than just how to draw this I suppose.

How do I define an l-system to use hexagons instead of squares or triangles?

• Hexagon = six triangles. – Symbol 1 Aug 19 '17 at 16:03
• How would I implement that into the code? If I make the hexagon out of 6 equilaterals, then with the standard sierpinski code I would end up with 6 copies of the triangle no? That would look pretty cool. – JSharpee Aug 19 '17 at 16:08
• Do it for me... – Dr. Manuel Kuehner Aug 19 '17 at 18:13

I simply adapt Jake's Sierpinski triangle : How to create a Sierpinski triangle in LaTeX?

## The code

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

• very nice, . .......... – Marek Kaski Aug 20 '17 at 5:41

Just for fun, I've reproduced this fractal figure with MetaPost, thanks to a recursive macro.

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.