% Copyright 2019 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.


\section{Turtle Graphics Library}
\label{section-library-tutrle}

\begin{tikzlibrary}{turtle}
    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.
    %
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}]
\tikz[turtle/distance=2mm]
  \draw [turtle={home,forward,right,forward,left,forward,left,forward}];
\end{codeexample}
    %
\end{tikzlibrary}

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) \emph{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.

\begin{key}{/tikz/turtle=\meta{keys}}
    This key executes the \meta{keys} with the current key path set to
    |/tikz/turtle|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}]
\tikz[turtle/distance=2mm]
  \draw [turtle={home,fd,rt,fd,lt,fd,lt,fd}];
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/turtle/home}
    Places the turtle at the origin and lets it head upward.
\end{key}

\begin{key}{/tikz/turtle/forward=\meta{distance} (default \normalfont see text)}
    Makes the turtle move forward by the given \meta{distance}. If no
    \meta{distance} is specified, the current value of the following key is
    used:
    %
    \begin{key}{/tikz/turtle/distance=\meta{distance} (initially 1cm)}
        The default distance by which the turtle advances.
    \end{key}
    %
    ``Moving forward the turtle'' actually means that, relative to the current
    last point on the path, a point at the given \meta{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.
    %
    \begin{stylekey}{/tikz/turtle/how (initially \normalfont empty)}
        This style can set up the |to path| used by turtles. By setting this
        style you can change the to-path:
        %
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}]
\tikz \draw [turtle={how/.style={bend left},home,forward,right,forward}];
\end{codeexample}
    \end{stylekey}
\end{key}

\begin{key}{/tikz/turtle/fd}
    An abbreviation for the |forward| key.
\end{key}

\begin{key}{/tikz/turtle/back=\meta{distance} (default \normalfont see text)}
    This has the same effect as a |turtle/forward| for the negated
    \meta{distance} value.
\end{key}

\begin{key}{/tikz/turtle/bk}
    An abbreviation for the |back| key.
\end{key}

\begin{key}{/tikz/turtle/left=\meta{angle} (default 90)}
    Turns the turtle left by the given angle.
\end{key}

\begin{key}{/tikz/turtle/lt}
    An abbreviation for the |left| key.
\end{key}

\begin{key}{/tikz/turtle/right=\meta{angle} (default 90)}
    Turns the turtle right by the given angle.
\end{key}

\begin{key}{/tikz/turtle/rt}
    An abbreviation for the |right| key.
\end{key}

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

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