Graphics, Figures & TablesQuadratic fractal with TikZ

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Tobias_G
Posts: 3
Joined: Sat May 04, 2013 5:27 pm

Quadratic fractal with TikZ

Post by Tobias_G »

Dear all,

I would like to create a fractal similar to the Koch snowflake using TikZ:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{tikzpicture}
\draw [l-system={rule set={F -> F-F++F-F}, step=2pt, angle=60,
   axiom=F++F++F, order=4}] lindenmayer system -- cycle;
\end{tikzpicture}
\end{document}
What do I have to change in the code above so that the outcome looks like the attached graphic? (One starts with a square of sidelength 1, then one attaches in the middle of each side a new square with sidelength 1/3 and so on...)

Thanks for your help!
Attachments
quadrat_fraktal.png
quadrat_fraktal.png (6.52 KiB) Viewed 5659 times
Last edited by cgnieder on Mon May 06, 2013 9:54 am, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Quadratic fractal with TikZ

Post by Stefan Kottwitz »

Hi Tobias,

the fractal image is built of 4 identical parts, so generate the basic part and built it together by an axiom like F+F+F+F with an angle of 90 degrees.

In contrast to the Koch snowflake, not each line is recursively replaced. This can be solved using two different rules.

So here's an approach:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{tikzpicture}
  \draw [l-system={rule set={G -> F-G+G+G-F, F -> FF}, step=2pt, angle=90,
         axiom=G+G+G+G, order=3}] lindenmayer system -- cycle;
\end{tikzpicture}
\end{document}
The versions up to level 4, scaling omitted:
fractal.png
fractal.png (3.79 KiB) Viewed 5681 times
It still seems to need a bit of adjusting line lengths, to exact match the image.

Stefan
LaTeX.org admin
Tobias_G
Posts: 3
Joined: Sat May 04, 2013 5:27 pm

Re: Quadratic fractal with TikZ

Post by Tobias_G »

Hi Stefan,

thank you very much for your quick an enlightening response!

How can I adjust the line lengths?

Tobias
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Quadratic fractal with TikZ

Post by Stefan Kottwitz »

Another rule is useful here:

Code: Select all

\draw [l-system={rule set={G -> F-G+G+G-F, F -> FFH, H -> FF}, step=2pt, angle=90,
       axiom=G+G+G+G, order=3}] lindenmayer system -- cycle;
Tested with order 3 and 5 (used \begin{tikzpicture}[scale=0.40,transform shape] to scale it down then)
square-fractal.png
square-fractal.png (4.72 KiB) Viewed 5662 times
Stefan
LaTeX.org admin
Tobias_G
Posts: 3
Joined: Sat May 04, 2013 5:27 pm

Re: Quadratic fractal with TikZ

Post by Tobias_G »

Stefan,

you are a LaTeX-BOSS !

Thank you so much for your help.
Tobias
Post Reply