turtle.tex 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. \documentstyle[11pt,epsfig]{article}
  2. \title{{\bf Turtle Graphics Interface for REDUCE \\
  3. Version 3}}
  4. \author{Caroline Cotter \\ Konrad--Zuse--Zentrum f\"ur Informationstechnik
  5. Berlin \\ E--mail: cotter@zib.de}
  6. \date{October 1998}
  7. \newcommand{\syntax}{\textsl{SYNTAX: }}
  8. \newcommand{\abb}{\textsl{Abbreviated form: }}
  9. \begin{document}
  10. \maketitle
  11. \section{Introduction}
  12. This program is a simple implementation of the ``Turtle Graphics''
  13. style of drawing graphs in {\small REDUCE}. The background and ideas of
  14. ``Turtle Graphics'' are outlined below.
  15. \subsection{Turtle Graphics}
  16. Turtle Graphics was originally developed in the 1960's as part of the
  17. LOGO system, and used in the classroom as an introduction to graphics
  18. and using computers to help with mathematics.
  19. The LOGO language was created as part of an experiment to test the idea
  20. that programming may be used as an educational discipline to teach children.
  21. It was first intended to be used for problem solving, for illustrating
  22. mathematical concepts usually difficult to grasp, and for creation of
  23. experiments with abstract ideas.
  24. At first LOGO had no graphics capabilities, but fast development enabled
  25. the incorporation of graphics, known as ``Turtle Graphics'' into the
  26. language. ``Turtle Graphics'' is regarded by many as the main use of
  27. LOGO.
  28. {\bf Main Idea:}\ \ To use simple commands directing a turtle, such as
  29. forward, back, turnleft, in order to construct pictures as opposed to
  30. drawing lines connecting cartesian coordinate points.
  31. The `turtle' is at all times determined by its state \{$x$,$y$,$a$,$p$\}-
  32. where $x$,$y$ determine its position in the {\mbox (x,y)-plane}, $a$
  33. determines the angle (which describes the direction the turtle is
  34. facing) and $p$ signals whether the pen is up or down (i.\ e.\ \ whether or
  35. not it is drawing on the paper).
  36. \section{Implementation}
  37. Some alterations to the original ``Turtle Graphics'' commands have been
  38. made in this implementation due to the design of the graphics package
  39. {\em gnuplot} used in {\small REDUCE}.
  40. \begin{itemize}
  41. \item It is not possible to draw lines individually and to see each
  42. seperate line as it is added to the graph since gnuplot
  43. automatically replaces the last graph each time it calls on
  44. the plot function.
  45. Thus the whole sequence of commands must be input together if
  46. the complete picture is to be seen.
  47. \item This implementation does not make use of the standard turtle
  48. commands `pen-up' or `pen-down' . Instead, `set' commands are
  49. included which allow the turtle to move without drawing a line.
  50. \item No facility is provided here to change the pen-colour, but gnuplot
  51. does have the capability to handle a few different colours (which
  52. could be included later).
  53. \item Many of the commands are long and difficult to type out repeatedly,
  54. therefore all the commands included under {\em `Turtle Functions'}
  55. (below) are listed alongside an equivalent abbreviated form.
  56. \item The user has no control over the range of output that can be seen
  57. on the screen since the gnuplot program automatically adjusts the
  58. picture to fit the window. Hence the size of each specified `step'
  59. the turtle takes in any direction is not a fixed unit of length,
  60. rather it is relative to the scale chosen by gnuplot.
  61. \end{itemize}
  62. \section{Turtle Functions}
  63. As previously mentioned, the turtle is determined at all times by its
  64. state \{$x$,$y$,$a$\}: its position on the \mbox{(x,y)-plane} and its
  65. angle($a$) - its {\em heading} - which determines the direction the
  66. turtle is facing, in degrees, relative anticlockwise to the positive
  67. x-axis.
  68. \subsection{User Setting Functions}
  69. \begin{description}
  70. \item[setheading] Takes a number as its argument and resets the heading
  71. to this number. If the number entered is negative or greater than
  72. or equal to 360 then it is automatically checked to lie between 0
  73. and 360.
  74. Returns the turtle position \{$x$,$y$\}
  75. \syntax\ {\tt setheading($\theta$)}
  76. \abb\ {\tt sh($\theta$)}
  77. \item[leftturn] The turtle is turned anticlockwise through the
  78. stated number of degrees. Takes a number as its argument and
  79. resets the heading by adding this number to the previous heading
  80. setting.
  81. Returns the turtle position \{$x$,$y$\}
  82. \syntax\ {\tt leftturn($\alpha$)}
  83. \abb\ {\tt slt($\alpha$)}
  84. \item[rightturn] Similar to {\tt leftturn}, but the turtle is turned
  85. clockwise through the stated number of degrees. Takes a number as
  86. its argument and resets the heading by subtracting this number from
  87. the previous heading setting.
  88. Returns the turtle position \{$x$,$y$\}
  89. \syntax\ {\tt rightturn($\beta$)}
  90. \abb\ {\tt srt($\beta$)}
  91. \item[setx] Relocates the turtle in the x direction. Takes a number as
  92. its argument and repositions the state of the turtle by changing its
  93. x-coordinate.
  94. Returns \{\}
  95. \syntax\ {\tt setx($x$)}
  96. \abb\ {\tt sx($x$)}
  97. \item[sety] Relocates the turtle in the y direction. Takes a number as
  98. its argument and repositions the state of the turtle by changing its
  99. y-coordinate.
  100. Returns \{\}
  101. \syntax\ {\tt sety($y$)}
  102. \abb\ {\tt sy($y$)}
  103. \item[setposition] Relocates the turtle from its current position to the
  104. new cartesian coordinate position described. Takes a pair of
  105. numbers as its arguments and repositions the state of the turtle by
  106. changing the x and y coordinates.
  107. Returns \{\}
  108. \syntax\ {\tt setposition($x$,$y$)}
  109. \abb\ {\tt spn($x$,$y$)}
  110. \item[setheadingtowards] Resets the heading so that the turtle is facing
  111. towards the given point, with respect to its current position on
  112. the coordinate axes. Takes a pair of numbers as its arguments and
  113. changes the heading, but the turtle stays in the same place.
  114. Returns the turtle position \{$x$,$y$\}
  115. \syntax\ {\tt setheadingtowards($x$,$y$)}
  116. \abb\ {\tt shto($x$,$y$)}
  117. \item[setforward] Relocates the turtle from its current position by
  118. moving forward (in the direction of its heading) the number of
  119. steps given. Takes a number as its argument and repositions the
  120. state of the turtle by changing the x and y coordinates.
  121. Returns \{\}
  122. \syntax\ {\tt setforward($n$)}
  123. \abb\ {\tt sfwd($n$)}
  124. \item[setback] As with {\tt setforward}, but moves back (in the opposite
  125. direction of its heading) the number of steps given.
  126. Returns \{\}
  127. \syntax\ {\tt setback($n$)}
  128. \abb\ {\tt sbk($n$)}
  129. \end{description}
  130. \subsection{Line-Drawing Functions}
  131. \begin{description}
  132. \item[forward] Moves the turtle forward (in the direction its heading)
  133. the number of steps given. Takes a number as its argument and draws
  134. a line from its current position to a new position on the
  135. coordinate plane. The x and y coordinates are reset to the new
  136. values.
  137. Returns the list of points
  138. \{ \{{\it old} $x$,{\it old} $y$\}, \{{\it new} $x$,{\it new} $y$\} \}
  139. \syntax\ {\tt forward($s$)}
  140. \abb\ {\tt fwd($s$)}
  141. \item[back] As with {\tt forward} except the turtle moves back (in the
  142. opposite direction to its heading) the number of steps given.
  143. Returns the list of points
  144. \{ \{{\it old} $x$,{\it old} $y$\}, \{{\it new} $x$,{\it new} $y$\} \}
  145. \syntax\ {\tt back($s$)}
  146. \abb\ {\tt bk($s$)}
  147. \item[move] Moves the turtle to a specified point on the coordinate
  148. plane. Takes a pair of numbers as its arguments and draws a line
  149. from its current position to the position described. The x and y
  150. coordinates are set to these new values.
  151. Returns the list of points
  152. \{ \{{\it old} $x$,{\it old} $y$\}, \{{\it new} $x$,{\it new} $y$\} \}
  153. \syntax\ {\tt move($x$,$y$)}
  154. \abb\ {\tt mv($x$,$y$)}
  155. \end{description}
  156. \subsection{Plotting Functions}
  157. \begin{description}
  158. \item[draw] This is the function the user calls within {\small REDUCE} to
  159. draw the list of turtle commands given into a picture. Takes a list
  160. as its argument, with each seperate command being seperated by a
  161. comma, and returns the graph drawn by following the commands.
  162. \syntax\ {\tt draw\{}{\it command(command\_args)}{\tt ,\ldots,}
  163. {\it command(command\_args)}{\tt \}}
  164. \underline{Note:} all commands may be entered in either long or
  165. shorthand form, and with a space
  166. before the arguments instead of parentheses only
  167. if just one argument is needed. Commands taking
  168. more than one argument must be written in
  169. parentheses and arguments seperated by a comma.
  170. \item[fdraw] This function is also called in {\small REDUCE} by the user
  171. and outputs the same as the {\tt draw} command, but it takes a
  172. filename as its argument. The file which is called upon by {\tt
  173. fdraw} must contain only the turtle commands and other functions
  174. defined by the user for turtle graphics. (This is intended to make
  175. it easier for the user to make small changes without constantly
  176. typing out long series of commands.)
  177. \syntax\ {\tt fdraw\{"}{\it filename}{\tt "\}}
  178. \underline{Note:} commands may be entered in long or shorthand form
  179. but each command must be written on a separate
  180. line of the file. Also, arguments are to be
  181. written without parentheses and separated with a
  182. space, not a comma, regardless of the number of
  183. arguments given to the function.
  184. \end{description}
  185. \subsection{Other Important Functions}
  186. \begin{description}
  187. \item[info] This function is called on its own in {\small REDUCE} to tell
  188. user the current state of the turtle. Takes no arguments but
  189. returns a list containing the current values of the x and y
  190. coordinates and the heading variable.
  191. Returns the list \{{\it x\_coord},{\it y\_coord},{\it heading}\}
  192. \syntax\ {\tt info()} or simply {\tt info}
  193. \item[clearscreen] This is also called on its own in {\small REDUCE} to
  194. get rid of the last gnuplot window, displaying the last turtle
  195. graphics picture, and to reset all the variables to 0. Takes no
  196. arguments and returns no printed output to the screen but the
  197. graphics window is simply cleared.
  198. \syntax\ {\tt clearscreen()} or simply {\tt clearscreen}
  199. \abb\ {\tt cls()} or {\tt cls}
  200. \item[home] This is a command which can be called within a plot function
  201. as well as outside of one. Takes no arguments, and simply resets
  202. the x and y coordinates and the heading variable to 0. When used in
  203. a series of turtle commands, it moves the turtle from its current
  204. position to the origin and sets the direction of the turtle along
  205. the x-axis, without drawing a line.
  206. Returns \{0,0\}
  207. \syntax\ {\tt home()} or simply {\tt home}
  208. \end{description}
  209. \subsection{Defining Functions}
  210. It is possible to use conditional statements (if \ldots\ then \ldots\
  211. else \ldots) and `for' statements (for i:=\ldots collect\{\ldots\}) in
  212. calls to draw. However, care must be taken - when using conditional
  213. statements the final else statement must return a point or at least
  214. \{x\_coord,y\_coord\} if the picture is to be continued at that point.
  215. Also, `for' statements {\em must} include `collect' followed by a list of
  216. turtle commands (in addition, the variable must begin counting from 0 if
  217. it is to be joined to the previous list of turtle commands at that point
  218. exactly, e.\ g.\ \ for i:=0:10 collect \{\ldots\}).
  219. \syntax\ \{\small {(For user-defined Turtle functions)}\}
  220. \begin{tabbing}
  221. \ \ \= set \ \ \ \=tabs\= \kill
  222. \> {\tt procedure {\it func\_name(func\_args)};} \\
  223. \> {\tt begin [scalar {\it additional variables}];} \\
  224. \> \> \vdots \\
  225. \> \> {\it (the procedure body containing some turtle commands)} \\
  226. \> \> \vdots \\
  227. \> \> {\tt return} {\it (a list, or label to a list, of turtle commands}\\
  228. \> \> \>{\it as accepted by {\tt draw})}\\
  229. \> {\tt end;} \\
  230. \end{tabbing}
  231. For convenience, it is recommended that all user defined functions, such
  232. as those involving {\tt if\ldots then\ldots else\ldots} or {\tt for
  233. i:=\ldots collect\{\ldots\}} are defined together in a separate file, then
  234. called into {\small REDUCE} using the {\tt in "{\it filename}"} command.
  235. \newpage
  236. \section{Examples}
  237. The following examples are taken from the tur.tst file.
  238. Examples 1,2,5 \& 6 are simple calls to draw. Examples 3 \& 4 show how
  239. more complicated commands can be built (which can take their own set of
  240. arguments) using procedures. Examples 7 \& 8 show the difference between
  241. the draw and fdraw commands.
  242. \begin{verbatim}
  243. % (1) Draw 36 rays of length 100
  244. draw {for i:=1:36 collect{setheading(i*10), forward 100, back 100} };
  245. \end{verbatim}
  246. \unitlength=1cm
  247. \begin{picture}(8,8)(0,0)
  248. \put (0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg1.ps}}}}
  249. \end{picture}
  250. \begin{verbatim}
  251. % (2) Draw 12 regular polygons with 12 sides of length 40,each polygon
  252. %forming an angle of 360/n degrees with the previous one.
  253. draw {for i:=1:12 collect
  254. {leftturn(30), for j:=1:12 collect
  255. {forward 40, leftturn(30)}} };
  256. \end{verbatim}
  257. \unitlength=1cm
  258. \begin{picture}(8,8)(0,0)
  259. \put (0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg4.ps}}}}
  260. \end{picture}
  261. \begin{verbatim}
  262. % (3) A "peak" pattern - an example of a recursive procedure.
  263. procedure peak(r);
  264. begin;
  265. return for i:=0:r collect
  266. {move(x_coord+5,y_coord-10), move(x_coord+10,y_coord+60),
  267. move(x_coord+10,y_coord-60),move(x_coord+5,y_coord+10)};
  268. end;
  269. draw {home(), peak(3)};
  270. \end{verbatim}
  271. \unitlength=1cm
  272. \begin{picture}(8,8)(0,0)
  273. \put (0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg5a.ps}}}}
  274. \end{picture}
  275. \begin{verbatim}
  276. %This procedure can then be part of a longer chain of commands:
  277. draw {home(), move(5,50), peak(3), move(x_coord+10,-100),
  278. peak(2), move(x_coord+10,0)};
  279. \end{verbatim}
  280. \unitlength=1cm
  281. \begin{picture}(8,8)(0,0)
  282. \put (0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg5b.ps}}}}
  283. \end{picture}
  284. \begin{verbatim}
  285. % (4) Write a recursive procedure which draws "trees" such that every
  286. %branch is half the length of the previous branch.
  287. procedure tree(a,b); %Here: a is the start length, b is the
  288. %number of levels
  289. begin;
  290. return if fixpb and b>0 %checking b is a positive integer
  291. then {leftturn(45), forward a, tree(a/2,b-1),
  292. back a, rightturn(90), forward a, tree(a/2,b-1),
  293. back a, leftturn(45)}
  294. else {x_coord,y_coord}; %default: Turtle stays still
  295. end;
  296. draw {home(), tree(130,7)};
  297. \end{verbatim}
  298. \unitlength=1cm
  299. \begin{picture}(8,8)(0,0)
  300. \put(0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg6a.ps}}}}
  301. \end{picture}
  302. \begin{verbatim}
  303. % (5) A 36-point star.
  304. draw {home(), for i:=1:36 collect
  305. {leftturn(10), forward 100, leftturn(10), back 100} };
  306. \end{verbatim}
  307. \unitlength=1cm
  308. \begin{picture}(8,8)(0,0)
  309. \put(0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg7.ps}}}}
  310. \end{picture}
  311. \begin{verbatim}
  312. % (6) Draw 100 equilateral triangles with the leading points
  313. %equally spaced on a circular path.
  314. draw {home(), for i:=1:100 collect
  315. {forward 150, rightturn(60), back(150),
  316. rightturn(60), forward 150, setheading(i*3.6)} };
  317. \end{verbatim}
  318. \unitlength=1cm
  319. \begin{picture}(8,8)(0,0)
  320. \put(0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg8.ps}}}}
  321. \end{picture}
  322. \begin{verbatim}
  323. % (7) Two or more graphs can be drawn together (this is easier
  324. %if the graphs are named). Here we show graphs 2 and 6 on top of one
  325. %another:
  326. gr2:={home(), for i:=1:12 collect
  327. {leftturn(30), for j:=1:12 collect
  328. {forward 40, leftturn(30)}} }$
  329. gr6:={home(), for i:=1:100 collect
  330. {forward 150, rightturn(60), back(150),
  331. rightturn(60), forward 150, setheading(i*3.6)} }$
  332. draw {gr2, gr6};
  333. \end{verbatim}
  334. \unitlength=1cm
  335. \begin{picture}(8,8)(0,0)
  336. \put(0,8){\rotatebox{270}{\resizebox{8cm}{8cm}{\epsfbox{eg9.ps}}}}
  337. \end{picture}
  338. \begin{verbatim}
  339. % (8) Example 7 could have been tackled another way, which makes use of
  340. %the fdraw command.
  341. %By inputting gr2 and gr6 as procedures into reduce, they can then be
  342. %used at any time in the same reduce session in a call to draw and even
  343. %fdraw.
  344. %First save the procedures in a file, say fxp (fdraw example procedures):
  345. procedure gr2;
  346. begin;
  347. return {home, for i:=1:12 collect
  348. {leftturn(30), for j:=1:12 collect
  349. {forward 40, leftturn(30)}} };
  350. end;
  351. procedure gr6;
  352. begin;
  353. return {home(), for i:=1:100 collect
  354. {forward 150, rightturn(60), back(150),
  355. rightturn(60), forward 150, setheading(i*3.6)} };
  356. end;
  357. %Then create another file where the functions may be called to fdraw,
  358. %e.g. fx:
  359. gr2
  360. gr6
  361. %Now in reduce, after loading the turtle package just type the following:
  362. in "fxp";
  363. fdraw '"fx";
  364. %..and the graphs will appear.
  365. %This method is useful if the user wants to define many of their own
  366. %functions, and, using fdraw, subtle changes can be made quickly without
  367. %having to type out the whole string of commands to plot each time. It
  368. %is particularly useful if there are several pictures to plot at once and
  369. %it is an easy way to build pictures so that the difference an extra
  370. %command makes to the overall picture can be clearly seen.
  371. %(In the above example, the file called to fdraw was only 2 lines long,
  372. %so this method did not have any advantage over the normal draw command.
  373. %However, when the list of commands is longer it is clearly advantageous
  374. %to use fdraw)
  375. \end{verbatim}
  376. \section{References}
  377. \begin{enumerate}
  378. \item {\bf An Implementation of Turtle Graphics for Teaching Purposes}\\
  379. Zoran I. Putnik \& Zoram d.Budimac
  380. \item {\bf Mapletech -} Maple in Mathematics and the Sciences,\\
  381. Special Issue 1994\\
  382. {\bf An Implementation of ``Turtle Graphics'' in Maple V}\\
  383. Eugenio Roanes Lozano \& Eugenio Roanes Macias
  384. \end{enumerate}
  385. \end{document}