TEXT   50

turtle object

Guest on 12th May 2022 01:15:32 AM

  1. <para>
  2.   As you've seen, we often build data structures for a particular kind
  3.   of data.  For example, in implementing turtle graphics, we might begin
  4.   by storing four pieces of information on each turtle.  (There are other
  5.   useful pieces of information, but these are four that can get us started.)
  6. </para>
  7.  
  8. <itemizedlist>
  9.   <listitem>
  10.     0: the image on which the turtle draws;
  11.   </listitem>
  12.   <listitem>
  13.     1: a real number for the x coordinate of the position of the turtle;
  14.   </listitem>
  15.   <listitem>
  16.     2: a real number for the y coordinate of the position of the turtle; and
  17.   </listitem>
  18.   <listitem>
  19.     3: a real number for the angle of the directions in which the turtle
  20.     is facing.
  21.   </listitem>
  22. </itemizedlist>
  23.  
  24. <para>
  25.   We then choose whether we want to store those attributes in a list
  26.   or a vector.  Suppose we store those components in a vector, with the
  27.   image at position 0, the x coordinate at position 1, the
  28.   y coordinate at position 2, and the angle at position 3.
  29. </para>
  30.  
  31. <para>
  32.   Once we've designed a representation, we could then tell other programmers
  33.   about this representation, and let them rely on the structure (e.g.,
  34.   someone who wants to change the angle of a turtle would do a
  35.   <code>vector-set!</code> at index 3).  
  36. </para>
  37.  
  38. <para>
  39.   However, suppose we change the representation (e.g., we design a
  40.   position type and decide to use that type to represent the position of
  41.   the turtle).  In that case other programmers must then change their
  42.   code to match the new representation.  Hence, it is often better to
  43.   provide a set of procedures to give other programmers access to the
  44.   key components of our structure.  If they use only our procedures,
  45.   then we can change the representation freely (as long as we change
  46.   our procedures correspondingly) and their code will continue to work.
  47. </para>
  48.  
  49. <para>
  50.   Here are some sample procedures for this simple type of turtle.
  51. </para>
  52.  
  53. <programlisting>
  54. (define make-turtle
  55.   (lambda (image)
  56.     (cond
  57.       ((not (image? image))
  58.        (error "make-turtle: parameter 0 (image) must be an image"))
  59.       (else
  60.        (vector image 0 0 0)))))
  61.  
  62. (define turtle-get-image
  63.   (lambda (turtle)
  64.     (vector-ref turtle 0)))
  65.  
  66. (define turtle-get-x
  67.   (lambda (turtle)
  68.     (vector-ref turtle 1)))
  69.  
  70. (define turtle-get-y
  71.   (lambda (turtle)
  72.     (vector-ref turtle 2)))
  73.  
  74. (define turtle-get-angle
  75.   (lambda (turtle)
  76.     (vector-ref turtle 3)))
  77.  
  78. (define turtle-set-angle
  79. </programlisting>
  80.  
  81. ...
  82.  
  83. (define turtle-set-angle!
  84.   (lambda (turtle new-angle)
  85.     (vector-set! turtle 3 new-angle)))
  86.  
  87. (define turtle-turn!
  88.   (lambda (turtle angle)
  89.     (vector-set! turtle 3 (+ (vector-ref turtle 3) angle))))
  90.  
  91. (define turtle-forward!
  92.   (let ((degrees-to-radians (/ pi 180)))
  93.     (lambda (turtle distance)
  94.       (let* ((image (vector-ref turtle 0))
  95.              (x (vector-ref turtle 1))
  96.              (y (vector-ref turtle 2))
  97.              (angle (vector-ref turtle 3))
  98.              (newx (+ x (cos angle)))
  99.              (newy (+ y (sin angle))))
  100.         (image-draw-line! image x y newx newy)
  101.         (vector-set! turtle 1 newx)
  102.         (vector-set! turtle 2 newy)))

Raw Paste


Login or Register to edit or fork this paste. It's free.