Debian Codegraph @

texmf-dist/doc/latex/lapdf/turtle.tex

raw
1 % ---------------------------------------------------------------------------
2 % Draws turtle graphics in pdfTeX with Lapdf. The procedures are taken
3 % from Abelson's introductory book for object logo on the Macintosh.
4 % ---------------------------------------------------------------------------
5 \input preamble.tex
6
7 \newcount\i
8 \newcount\pen
9 \newdimen\a
10 \newdimen\b
11 \newdimen\t
12 \newdimen\dir
13 \newdimen\ax
14 \newdimen\tx
15 \newdimen\ty
16 \newdimen\ux
17 \newdimen\uy
18
19 % ---------------------------------------------------------------------------
20 % This is a simple Lapdf implementation of turtle graphics. The commands
21 % are: Initturtle, Home, Penup, Pendown, Forward, Back, Right, Left.
22 % ---------------------------------------------------------------------------
23 \def\Home{\Moveto(0,0)}
24 \def\Penup{\pen=0}
25 \def\Pendown{\pen=1}
26
27 \def\Initturtle{\Home \col=-1 \Dset(\dir,0) \Setwidth(0.02) \Pendown}
28
29 \def\Forward(#1){\Dset(\ax,#1) \Rad(\Np\dir,\ty)
30 \Sin(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\ux,\tx)
31 \Cos(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\uy,\tx)
32 \ifnum\pen=0 \Moveto(\Np\ux,\Np\uy) \else \Lineto(\Np\ux,\Np\uy) \fi}
33
34 \def\Back(#1){\Dset(\ax,#1) \Rad(\Np\dir,\ty)
35 \Sin(\Np\ty,\tx) \Dmul(\tx,\ax) \Sub(\ux,\tx)
36 \Cos(\Np\ty,\tx) \Dmul(\tx,\ax) \Add(\uy,\tx)
37 \ifnum\pen=0 \Moveto(\Np\ux,\Np\uy) \else \Lineto(\Np\ux,\Np\uy) \fi}
38
39 \def\Right(#1){\Dset(\ax,#1) \Add(\dir,\ax) {\Dmod(\dir,360)}}
40 \def\Left(#1){\Dset(\ax,#1) \Sub(\dir,\ax) {\Dmod(\dir,360)}}
41
42 % ---------------------------------------------------------------------------
43 % These are our own special drawing macros.
44 % ---------------------------------------------------------------------------
45 \def\Arcleft(#1,#2){\i=0 \Dset(\t,#1) \Dmul(\t,0.017453pt)
46 \Whilenum{\i<#2}{\Forward(\Np\t) \Left(1) \Add(\i,1)}}
47
48 \def\Arcright(#1,#2){\i=0 \Dset(\t,#1) \Dmul(\t,0.017453pt)
49 \Whilenum{\i<#2}{\Forward(\Np\t) \Right(1) \Add(\i,1)}}
50
51 \def\Fullcirc(#1){{\Arcright(#1,360)} \Stroke}
52
53 \def\Square(#1){\i=0
54 \Whilenum{\i<4}{\Forward(#1) \Right(90) \Add(\i,1)} \Stroke \Home}
55
56 \def\Rosette(#1,#2){\i=0 \Dset(\t,360) \Div(\t,#2)
57 \Whilenum{\i<#2}{\Nextcol(0,23) {\Square(#1)} \Right(\Np\t) \Add(\i,1)}}
58
59 \def\Petal(#1){\Arcright(#1,60) \Right(120) \Arcright(#1,60)
60 \Right(120) \Stroke \Home}
61
62 \def\Flower(#1){\i=0
63 \Whilenum{\i<6}{\Stepcol(0,23,4) {\Petal(#1)} \Right(60) \Add(\i,1)}}
64
65 % ---------------------------------------------------------------------------
66 \begin{document}
67 \unitlength1.25cm
68
69 \begin{center}
70 {\Huge\bf{Turtle Graphics with \Lapdf}}
71 \bigskip
72
73 \begin{lapdf}(9,9)(-4.5,-4.5)
74 \Initturtle
75 \Rosette(3,24)
76 \Initturtle
77 \Penup
78 \Forward(4.243)
79 \Right(90)
80 \Pendown
81 \Dgray
82 \Fullcirc(4.243)
83 \end{lapdf}
84
85 \begin{lapdf}(9,9)(-4.5,-4.5)
86 \Initturtle
87 \Flower(5)
88 \end{lapdf}
89 \end{center}
90 \end{document}