| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Turtle Graphics Library} |
| 12 |
\label{section-library-tutrle} |
| 13 |
|
| 14 |
\begin{tikzlibrary}{turtle} |
| 15 |
This little library defines some keys to create simple turtle graphics in |
| 16 |
the tradition of the Logo programming language. These commands are mostly |
| 17 |
for fun, but they can also be used for more ``serious'' business. |
| 18 |
% |
| 19 |
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}] |
| 20 |
\tikz[turtle/distance=2mm] |
| 21 |
\draw [turtle={home,forward,right,forward,left,forward,left,forward}]; |
| 22 |
\end{codeexample} |
| 23 |
% |
| 24 |
\end{tikzlibrary} |
| 25 |
|
| 26 |
Even though the |turtle| keys looks like an option, it uses the |insert path| |
| 27 |
option internally to produce a path. |
| 28 |
|
| 29 |
The basic drawing model behind the turtle graphics is very simple: There is a |
| 30 |
(virtual) \emph{turtle} that crawls around the page, thereby extending the |
| 31 |
path. The turtle always heads in a certain direction. When you move the turtle |
| 32 |
forward, you extend the path in that direction; turning the turtle just changes |
| 33 |
the direction, it does not cause anything to be drawn. |
| 34 |
|
| 35 |
The turtle always moves relative to the last current point of the path and you |
| 36 |
can mix normal path commands with turtle commands. However, the direction of |
| 37 |
the turtle is managed independently of other path commands. |
| 38 |
|
| 39 |
\begin{key}{/tikz/turtle=\meta{keys}} |
| 40 |
This key executes the \meta{keys} with the current key path set to |
| 41 |
|/tikz/turtle|. |
| 42 |
% |
| 43 |
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}] |
| 44 |
\tikz[turtle/distance=2mm] |
| 45 |
\draw [turtle={home,fd,rt,fd,lt,fd,lt,fd}]; |
| 46 |
\end{codeexample} |
| 47 |
% |
| 48 |
\end{key} |
| 49 |
|
| 50 |
\begin{key}{/tikz/turtle/home} |
| 51 |
Places the turtle at the origin and lets it head upward. |
| 52 |
\end{key} |
| 53 |
|
| 54 |
\begin{key}{/tikz/turtle/forward=\meta{distance} (default \normalfont see text)} |
| 55 |
Makes the turtle move forward by the given \meta{distance}. If no |
| 56 |
\meta{distance} is specified, the current value of the following key is |
| 57 |
used: |
| 58 |
% |
| 59 |
\begin{key}{/tikz/turtle/distance=\meta{distance} (initially 1cm)} |
| 60 |
The default distance by which the turtle advances. |
| 61 |
\end{key} |
| 62 |
% |
| 63 |
``Moving forward the turtle'' actually means that, relative to the current |
| 64 |
last point on the path, a point at the given \meta{distance} in the |
| 65 |
direction the turtle is currently heading is computed. Then, the operation |
| 66 |
|to[turtle/how]| is used to extend the path to this point. |
| 67 |
% |
| 68 |
\begin{stylekey}{/tikz/turtle/how (initially \normalfont empty)} |
| 69 |
This style can set up the |to path| used by turtles. By setting this |
| 70 |
style you can change the to-path: |
| 71 |
% |
| 72 |
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}] |
| 73 |
\tikz \draw [turtle={how/.style={bend left},home,forward,right,forward}]; |
| 74 |
\end{codeexample} |
| 75 |
\end{stylekey} |
| 76 |
\end{key} |
| 77 |
|
| 78 |
\begin{key}{/tikz/turtle/fd} |
| 79 |
An abbreviation for the |forward| key. |
| 80 |
\end{key} |
| 81 |
|
| 82 |
\begin{key}{/tikz/turtle/back=\meta{distance} (default \normalfont see text)} |
| 83 |
This has the same effect as a |turtle/forward| for the negated |
| 84 |
\meta{distance} value. |
| 85 |
\end{key} |
| 86 |
|
| 87 |
\begin{key}{/tikz/turtle/bk} |
| 88 |
An abbreviation for the |back| key. |
| 89 |
\end{key} |
| 90 |
|
| 91 |
\begin{key}{/tikz/turtle/left=\meta{angle} (default 90)} |
| 92 |
Turns the turtle left by the given angle. |
| 93 |
\end{key} |
| 94 |
|
| 95 |
\begin{key}{/tikz/turtle/lt} |
| 96 |
An abbreviation for the |left| key. |
| 97 |
\end{key} |
| 98 |
|
| 99 |
\begin{key}{/tikz/turtle/right=\meta{angle} (default 90)} |
| 100 |
Turns the turtle right by the given angle. |
| 101 |
\end{key} |
| 102 |
|
| 103 |
\begin{key}{/tikz/turtle/rt} |
| 104 |
An abbreviation for the |right| key. |
| 105 |
\end{key} |
| 106 |
|
| 107 |
Turtle graphics are especially nice in conjunction with the |\foreach| |
| 108 |
statement: |
| 109 |
|
| 110 |
\begin{codeexample}[preamble={\usetikzlibrary{turtle}}] |
| 111 |
\tikz \filldraw [thick,blue,fill=blue!20] |
| 112 |
[turtle=home] |
| 113 |
\foreach \i in {1,...,5} |
| 114 |
{ |
| 115 |
[turtle={forward,right=144}] |
| 116 |
}; |
| 117 |
\end{codeexample} |