Debian Codegraph @

texmf-dist/dvips/pst-turtle/pst-turtle.pro

raw
1 % turtle.ps -- graphics turtle.
2 % @author Eric Laroche
3 % @version @(#)$Id: pst-turtle.pro 1092 2019-10-01 20:19:35Z herbert $
4 %
5 % turtle.ps -- graphics turtle.
6 % Copyright (C) 1997-2001 Eric Laroche.
7 %
8 % This program is free software;
9 % you can redistribute it and/or modify it.
10 %
11 % This program is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 %
15 % the '(graphics) turtle' is a 'cursor' that has a position (x and y
16 % coordinate), a heading and a pen up/down state. procedures as
17 % forward, left, etc. are used as drawing idioms.
18 %
19 % the turtle object consists of: cx cy phi penstate. cx and cy are the
20 % initialial coordinates, established when creating the turtle, they are
21 % the turtles zero coordinates. phi is the turtle heading, zero means
22 % in x direction (i.e. to the right), 90 means in the y direction (i.e.
23 % up). penstate is a boolean indicating whether the pen is down (true)
24 % or up (false).
25 % implicit to the turtle object is: currentpoint.
26 %
27 % note that using path constructing operators moveto, rmoveto, lineto,
28 % rlineto, arc, etc. will affect the turtle's state in a way that the
29 % turtle's current point is changed together with the postscript vm's
30 % current point by these operators. contrary the turtle's heading
31 % isn't affected by these operators.
32 %
33 % the turtle procedures indicate syntaxes using <turtle> (an array
34 % containing the turtle state) as abstract turtle object.
35 %
36 % no syntax is indicated with the abbreviated function names.
37 %
38 % these procedures are not handling newpath, closepath,
39 % setlinewidth, setlinejoin, setlinecap, stroke, fill, etc.
40 %
41 % a current point must be set before using the turtle object and after
42 % starting a new path. i.e. a typical initialization sequence would be
43 % e.g. 'newpath 200 200 moveto turtle 90 setheading'.
44 %
45 % create a turtle object.
46 % this saves the current point as zero point to return to by 'home'.
47 % a turtle object is an array of: [cx cy phi penstate].
48 % - turtle -> <turtle>
49 %
50 % on stack: { x y } boolean N@name type InitXnode
51
52 /tx@TurtleDict 50 dict def
53 tx@TurtleDict begin
54 %
55 /nodeCounter 0 def
56 /nodeCnt++ { nodeCounter 1 add /nodeCounter exch def } def
57
58 %/saveNode {
59 % { currentpoint }
60 % false
61 % (N@Turtle) nodeCounter 2 string cvs concatstrings 32 rightTrim cvn
62 % 10
63 % {InitPnode}
64 % tx@NodeDict begin NewNode end
65 % nodeCnt++
66 %} def
67
68 /saveNode {
69 tx@Dict begin
70 (Turtle) nodeCounter 2 string cvs concatstrings 32 rightTrim cvn { currentpoint } def
71 end
72 } def
73 %
74 /turtle {
75 % get initial position.
76 % initial position is current point.
77 2 setlinejoin
78 currentpoint
79 % phi and penstate
80 0 true
81 % note: not doing any newpath.
82 4 array astore
83 } bind def
84
85 % destroy a turtle object.
86 % note: this procedure serves for compatibility
87 % to older turtle formats.
88 % <turtle> unturtle -> -
89 /unturtle {
90 % return to turtle object's initial position.
91 dup 0 get
92 1 index 1 get % <turtle> cx cy
93 moveto
94 % note: not doing any newpath.
95 pop % -
96 } bind def
97
98 % return turtle object size.
99 % note: this procedure serves for compatibility
100 % to older turtle formats.
101 % - turtlesize -> 1
102 /turtlesize {
103 % 1 entry on the stack (an array with cx cy phi penstate).
104 1
105 } bind def
106
107 % normalize direction. phi must be >= 0 and < 360.
108 % <turtle> normalizephi -> <turtle>
109 /normalizephi {
110 dup 2 get % <turtle> phi
111 % note: denormalized typically by less than two circles,
112 % so the loops won't take too long.
113 % cut while larger or equal 2 pi.
114 {
115 dup 360 % <turtle> phi phi 360
116 lt {
117 exit
118 } if
119 360 sub
120 } loop
121 % increment while less zero.
122 {
123 dup 0 % <turtle> phi phi 0
124 ge {
125 exit
126 } if
127 360 add
128 } loop
129 exch dup 2
130 4 -1 roll
131 put % <turtle>
132 } bind def
133
134 % relatively move to, action depending on penstate.
135 % <turtle> dx dy pmoveto -> <turtle>
136 /pmoveto {
137 % move or draw, depending on penstate
138 2 index 3 get {
139 rlineto
140 } {
141 rmoveto
142 } ifelse
143 } def
144
145 % where we are, from initial point (in setxy coordinates).
146 % <turtle> xy -> <turtle> x y
147 /xy {
148 currentpoint % <turtle> x' y'
149 exch 2 index 0 get sub % <turtle> y' x
150 exch 2 index 1 get sub % <turtle> x y
151 } bind def
152
153 % calculate relative movements from setxy coordinates.
154 % <turtle> x y rxy -> <turtle> x' y'
155 /rxy {
156 3 -1 roll % x y <turtle>
157 xy % x y <turtle> x'' y''
158 5 -2 roll % <turtle> x'' y'' x y
159 exch 4 -1 roll sub
160 exch 3 -1 roll sub % <turtle> x' y'
161 } bind def
162
163
164 % logo language turtle functions
165
166 % put penstate in 'up' position.
167 % logo language turtle function
168 % <turtle> penup -> <turtle>
169 /penup {
170 % false means 'up'.
171 dup 3 false put
172 } bind def
173
174 % put penstate in 'down' position.
175 % logo language turtle function
176 % <turtle> pendown -> <turtle>
177 /pendown {
178 % true means 'down'.
179 dup 3 true put
180 } bind def
181
182 % advance in current direction.
183 % logo language turtle function
184 % <turtle> d forward -> <turtle>
185 /forward {
186 % dx is d cos phi.
187 1 index 2 get cos 1 index mul % <turtle> d dx
188 % dy is d sin phi.
189 2 index 2 get sin 3 -1 roll mul % <turtle> dx dy
190 pmoveto
191 saveNode
192 } def
193
194 % back up in current direction.
195 % logo language turtle function
196 % <turtle> d back -> <turtle>
197 /back {
198 neg
199 forward
200 } bind def
201
202 % change direction to the left (counterclockwise, positive direction).
203 % logo language turtle function
204 % <turtle> omega left -> <turtle>
205 /left {
206 1 index 2 get % <turtle> omega phi
207 add % <turtle> phi'
208 1 index 2 3 -1 roll put
209 normalizephi
210 } bind def
211
212 % change direction to the right (clockwise, negative direction).
213 % logo language turtle function
214 % <turtle> omega right -> <turtle>
215 /right {
216 neg
217 left
218 } bind def
219
220 % move to a specified point.
221 % logo language turtle function
222 % <turtle> x y setxy -> <turtle>
223 /setxy {
224 rxy % <turtle> x' y'
225 pmoveto % <turtle>
226 } bind def
227
228 % move to a specified point (only x changes).
229 % logo language turtle function
230 % <turtle> x setx -> <turtle>
231 /setx {
232 0 rxy % <turtle> x' y'
233 pop 0 pmoveto % <turtle>
234 } bind def
235
236 % move to a specified point (only y changes).
237 % logo language turtle function
238 % <turtle> y sety -> <turtle>
239 /sety {
240 0 exch rxy % <turtle> x' y'
241 exch pop 0 exch pmoveto % <turtle>
242 } bind def
243
244 % set the heading.
245 % logo language turtle function
246 % <turtle> phi' setheading -> <turtle>
247 /setheading { 1 index 2 3 -1 roll put normalizephi } bind def
248
249 % set heading towards a point.
250 % logo language turtle function
251 % <turtle> x y towards -> <turtle>
252 /towards {
253 rxy % <turtle> x' y'
254 % check if both zero.
255 1 index 0 eq 1 index 0 eq and {
256 % set heading to zero.
257 pop pop 0
258 } {
259 exch atan
260 } ifelse
261 setheading
262 } bind def
263
264 % go home; heading to zero.
265 % logo language turtle function
266 % <turtle> home -> <turtle>
267 /home { 0 dup setxy 0 setheading } bind def
268
269 % get x coordinate.
270 % logo language turtle function
271 % <turtle> xcor -> cx <turtle> x
272 /xcor { xy pop } bind def
273
274 % get y coordinate.
275 % logo language turtle function
276 % <turtle> ycor -> <turtle> y
277 /ycor { xy exch pop } bind def
278
279 % get heading.
280 % logo language turtle function
281 % <turtle> heading -> <turtle> phi
282 /heading { dup 2 get } bind def
283
284 % get pen state.
285 % logo language turtle function
286 % note: only the turtle relevant stuff given.
287 % <turtle> drawstate -> <turtle> penstate
288 /drawstate { dup 3 get } bind def
289 %
290 % logo language turtle function abbreviations
291 %
292 /bk { back } bind def
293 /fd { forward } bind def
294 /lt { left } bind def
295 /pd { pendown } bind def
296 /pu { penup } bind def
297 /rt { right } bind def
298 /seth { setheading } bind def
299 %
300 end
301 % end of pst-turtle.pro
302