Graphics, Figures & TablestikZ | Koch Snowflake

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

tikZ | Koch Snowflake

Post by svend_tveskaeg »

Hi all.

Consider the following MWE, which I found here:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}
\begin{document}
\begin{tikzpicture}[decoration=Koch snowflake]
    \draw decorate{ decorate{ decorate{ decorate{ (0,0) -- (3,0) }}}};
\end{tikzpicture}
\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)

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

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

tikZ | Koch Snowflake

Post by Stefan Kottwitz »

Hi,

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

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}
\begin{document}
\begin{tikzpicture}[decoration=Koch snowflake]
  \draw decorate{ decorate{ decorate{ decorate{
       (0,0) -- ++(60:3)  -- ++(300:3) -- ++(180:3)}}}};
\end{tikzpicture}
\end{document}
snowflake.png
snowflake.png (6.17 KiB) Viewed 12475 times
Stefan
LaTeX.org admin
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: tikZ | Koch Snowflake

Post by svend_tveskaeg »

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

tikZ | Koch Snowflake

Post by Stefan Kottwitz »

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

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\usetikzlibrary[shadings]
\begin{document}
\begin{tikzpicture}
\shadedraw[shading=color wheel] 
  [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}
koch-snowflake.png
koch-snowflake.png (8.52 KiB) Viewed 12474 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

Re: tikZ | Koch Snowflake

Post by svend_tveskaeg »

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

tikZ | Koch Snowflake

Post by Stefan Kottwitz »

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

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}
koch.png
koch.png (3.93 KiB) Viewed 12465 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

Re: tikZ | Koch Snowflake

Post by svend_tveskaeg »

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)
Post Reply