PGF/TikZ Manual

The TikZ and PGF Packages
Manual for version 3.1.9a

Libraries

77 Turtle Graphics Library

  • TikZ Library turtle

  • \usetikzlibrary{turtle} % and plain
    \usetikzlibrary[turtle] % Cont

    This little library defines some keys to create simple turtle graphics in the tradition of the Logo programming language. These commands are mostly for fun, but they can also be used for more “serious” business.

    (-tikz- diagram)

    \usetikzlibrary {turtle}
    \tikz[turtle/distance=2mm]
    \draw [turtle={home,forward,right,forward,left,forward,left,forward}];

Even though the turtle keys looks like an option, it uses the insert path option internally to produce a path.

The basic drawing model behind the turtle graphics is very simple: There is a (virtual) turtle that crawls around the page, thereby extending the path. The turtle always heads in a certain direction. When you move the turtle forward, you extend the path in that direction; turning the turtle just changes the direction, it does not cause anything to be drawn.

The turtle always moves relative to the last current point of the path and you can mix normal path commands with turtle commands. However, the direction of the turtle is managed independently of other path commands.

  • /tikz/turtle/home(no value)

  • Places the turtle at the origin and lets it head upward.

  • /tikz/turtle/forward=distance (default see text)

  • Makes the turtle move forward by the given distance. If no distance is specified, the current value of the following key is used:

    • /tikz/turtle/distance=distance (no default, initially 1cm)

    • The default distance by which the turtle advances.

    “Moving forward the turtle” actually means that, relative to the current last point on the path, a point at the given distance in the direction the turtle is currently heading is computed. Then, the operation to[turtle/how] is used to extend the path to this point.

    • /tikz/turtle/how(style, initially empty)

    • This style can set up the to path used by turtles. By setting this style you can change the to-path:

      (-tikz- diagram)

      \usetikzlibrary {turtle}
      \tikz \draw [turtle={how/.style={bend left},home,forward,right,forward}];
  • /tikz/turtle/fd(no value)

  • An abbreviation for the forward key.

  • /tikz/turtle/back=distance (default see text)

  • This has the same effect as a turtle/forward for the negated distance value.

  • /tikz/turtle/bk(no value)

  • An abbreviation for the back key.

  • /tikz/turtle/left=angle (default 90)

  • Turns the turtle left by the given angle.

  • /tikz/turtle/lt(no value)

  • An abbreviation for the left key.

  • /tikz/turtle/right=angle (default 90)

  • Turns the turtle right by the given angle.

  • /tikz/turtle/rt(no value)

  • An abbreviation for the right key.

Turtle graphics are especially nice in conjunction with the \foreach statement:

(-tikz- diagram)

\usetikzlibrary {turtle}
\tikz \filldraw [thick,blue,fill=blue!20]
[turtle=home]
\foreach \i in {1,...,5}
{
[turtle={forward,right=144}]
};