summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-library-turtle.tex
blob: ffb35b231d4b94d114c5b971711ca7672d9cea58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
% Copyright 2008 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{pgflibrary}{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}[]
\tikz[turtle/distance=2mm]
  \draw [turtle={home,forward,right,forward,left,forward,left,forward}];
\end{codeexample}
\end{pgflibrary}

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}[]
\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 setup the |to path| used by turtles. By setting
    this style you can change the to-path: 
\begin{codeexample}[]
\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}[]
\tikz \filldraw [thick,blue,fill=blue!20]
  [turtle=home]
  \foreach \i in {1,...,5}
  {
    [turtle={forward,right=144}]
  };
\end{codeexample}

\endinput