summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lapdf/turtle.tex
blob: 88c472042745b76fbd05913ef5b9f5e6cdfddfbd (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
% ---------------------------------------------------------------------------
% Draws turtle graphics in pdfTeX with Lapdf. The procedures are taken
% from Abelson's introductory book for object logo on the Macintosh.
% ---------------------------------------------------------------------------
\input preamble.tex

\newcount\i
\newcount\pen
\newdimen\a
\newdimen\b
\newdimen\t
\newdimen\dir
\newdimen\ax
\newdimen\tx
\newdimen\ty
\newdimen\ux
\newdimen\uy

% ---------------------------------------------------------------------------
% This is a simple Lapdf implementation of turtle graphics. The commands
% are: Initturtle, Home, Penup, Pendown, Forward, Back, Right, Left.
% ---------------------------------------------------------------------------
\def\Home{\Moveto(0,0)}
\def\Penup{\pen=0}
\def\Pendown{\pen=1}

\def\Initturtle{\Home \col=-1 \Dset(\dir,0) \Setwidth(0.02) \Pendown}

\def\Forward(#1){\Dset(\ax,#1) \Rad(\Np\dir,\ty)
 \Sin(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\ux,\tx)
 \Cos(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\uy,\tx)
 \ifnum\pen=0 \Moveto(\Np\ux,\Np\uy) \else \Lineto(\Np\ux,\Np\uy) \fi}

\def\Back(#1){\Dset(\ax,#1) \Rad(\Np\dir,\ty)
 \Sin(\Np\ty,\tx) \Dmul(\tx,\ax) \Sub(\ux,\tx)
 \Cos(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\uy,\tx)
 \ifnum\pen=0 \Moveto(\Np\ux,\Np\uy) \else \Lineto(\Np\ux,\Np\uy) \fi}

\def\Right(#1){\Dset(\ax,#1) \Add(\dir,\ax) {\Dmod(\dir,360)}}
\def\Left(#1){\Dset(\ax,#1) \Sub(\dir,\ax) {\Dmod(\dir,360)}}

% ---------------------------------------------------------------------------
% These are our own special drawing macros.
% ---------------------------------------------------------------------------
\def\Arcleft(#1,#2){\i=0 \Dset(\t,#1) \Dmul(\t,0.017453pt)
 \Whilenum{\i<#2}{\Forward(\Np\t) \Left(1) \Add(\i,1)}}

\def\Arcright(#1,#2){\i=0 \Dset(\t,#1) \Dmul(\t,0.017453pt)
 \Whilenum{\i<#2}{\Forward(\Np\t) \Right(1) \Add(\i,1)}}

\def\Fullcirc(#1){{\Arcright(#1,360)} \Stroke}

\def\Square(#1){\i=0
 \Whilenum{\i<4}{\Forward(#1) \Right(90) \Add(\i,1)} \Stroke \Home}

\def\Rosette(#1,#2){\i=0 \Dset(\t,360) \Div(\t,#2)
 \Whilenum{\i<#2}{\Nextcol(0,23) {\Square(#1)} \Right(\Np\t) \Add(\i,1)}}

\def\Petal(#1){\Arcright(#1,60) \Right(120) \Arcright(#1,60)
 \Right(120) \Stroke \Home}

\def\Flower(#1){\i=0
 \Whilenum{\i<6}{\Stepcol(0,23,4) {\Petal(#1)} \Right(60) \Add(\i,1)}}

% ---------------------------------------------------------------------------
\begin{document}
\unitlength1.25cm

\begin{center}
{\Huge\bf{Turtle Graphics with \Lapdf}}
\bigskip

\begin{lapdf}(9,9)(-4.5,-4.5)
 \Initturtle
 \Rosette(3,24)
 \Initturtle
 \Penup
 \Forward(4.243)
 \Right(90)
 \Pendown
 \Dgray
 \Fullcirc(4.243)
\end{lapdf}

\begin{lapdf}(9,9)(-4.5,-4.5)
 \Initturtle
 \Flower(5)
\end{lapdf}
\end{center}
\end{document}