LaTeX forum ⇒ Graphics, Figures & TablestikZ | Koch Snowflake Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

tikZ | Koch Snowflake

Postby svend_tveskaeg » Sun Feb 12, 2012 4:46 pm

Hi all.

Consider the following MWE, which I found here:

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{decorations.fractals}
  4. \begin{document}
  5. \begin{tikzpicture}[decoration=Koch snowflake]
  6. \draw decorate{ decorate{ decorate{ decorate{ (0,0) -- (3,0) }}}};
  7. \end{tikzpicture}
  8. \end{document}

How do I extend it in order to draw the entire fractal?

Thank you in advance!

P.S. I have never used TikZ before.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

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

Postby Stefan Kottwitz » Sun Feb 12, 2012 5:05 pm

Hi,

decorate a longer path. Here I use angles and relative coordinates:

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{decorations.fractals}
  4. \begin{document}
  5. \begin{tikzpicture}[decoration=Koch snowflake]
  6. \draw decorate{ decorate{ decorate{ decorate{
  7. (0,0) -- ++(60:3) -- ++(300:3) -- ++(180:3)}}}};
  8. \end{tikzpicture}
  9. \end{document}


snowflake.png
snowflake.png (6.17 KiB) Viewed 7282 times


Stefan
LaTeX.org admin

User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Postby svend_tveskaeg » Sun Feb 12, 2012 5:10 pm

Spot on! Thank you!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

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

Postby Stefan Kottwitz » Sun Feb 12, 2012 5:13 pm

You could also use the lindenmayersystems library. Here's an example with additional color shading, which I posted on my TeX blog:

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{lindenmayersystems}
  4. \usetikzlibrary[shadings]
  5. \begin{document}
  6. \begin{tikzpicture}
  7. \shadedraw[shading=color wheel]
  8. [l-system={rule set={F -> F-F++F-F}, step=2pt, angle=60,
  9. axiom=F++F++F, order=4}] lindenmayer system -- cycle;
  10. \end{tikzpicture}
  11. \end{document}


koch-snowflake.png
koch-snowflake.png (8.52 KiB) Viewed 7281 times


More of this: Lindenmayer systems with TikZ.

Stefan
LaTeX.org admin

User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Postby svend_tveskaeg » Mon Feb 13, 2012 12:02 pm

Thank you!

I did notice this example at first, but I would like the drawing to be "clean". Therefore, I chose the colorless snowflake.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

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

Postby Stefan Kottwitz » Mon Feb 13, 2012 12:28 pm

No problem, the shading was just a fancy addition. You could use a normal \draw.

  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}


koch.png
koch.png (3.93 KiB) Viewed 7272 times


With the L-System syntax we could draw various fractals, both well known as the Sierpinski gasket, the space-filling fractals Pascal curve and Hilbert curve, but many more also unknown trees, farns and other plan structures, ornaments, and more.

Stefan
LaTeX.org admin

User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Postby svend_tveskaeg » Mon Feb 13, 2012 1:57 pm

Arrrh! I see.

Then I will implement that instead.

Thank you for the hint(s)!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 14 guests