LaTeX forum ⇒ Graphics, Figures & TablesQuadratic fractal with TikZ

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

Quadratic fractal with TikZ

Postby Tobias_G » Sun May 05, 2013 1:24 pm

Dear all,

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

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{lindenmayersystems}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \draw [l-system={rule set={F -> F-F++F-F}, step=2pt, angle=60,
  7. axiom=F++F++F, order=4}] lindenmayer system -- cycle;
  8. \end{tikzpicture}
  9. \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 3871 times
Last edited by cgnieder on Mon May 06, 2013 9:54 am, edited 2 times in total.
Reason: inline code changed to code block, scaled image so it can also be viewed on small screens

Tags:

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

Postby Stefan Kottwitz » Sun May 05, 2013 4:20 pm

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:

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{lindenmayersystems}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \draw [l-system={rule set={G -> F-G+G+G-F, F -> FF}, step=2pt, angle=90,
  7. axiom=G+G+G+G, order=3}] lindenmayer system -- cycle;
  8. \end{tikzpicture}
  9. \end{document}


The versions up to level 4, scaling omitted:

fractal.png
fractal.png (3.79 KiB) Viewed 3893 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

Postby Tobias_G » Sun May 05, 2013 10:49 pm

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: 9572
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Mon May 06, 2013 9:20 am

Another rule is useful here:

  1. \draw [l-system={rule set={G -> F-G+G+G-F, F -> FFH, H -> FF}, step=2pt, angle=90,
  2. 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 3874 times


Stefan
LaTeX.org admin

Tobias_G
Posts: 3
Joined: Sat May 04, 2013 5:27 pm

Postby Tobias_G » Mon May 06, 2013 6:03 pm

Stefan,

you are a LaTeX-BOSS !

Thank you so much for your help.
Tobias


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests