turtle3.body.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (define (image-rotate x y theta1 delta-theta)
  2. (when canvas-image-rotate
  3. (canvas-image-rotate x y theta1 delta-theta)))
  4. (define (line-color color)
  5. (if canvas-line-color
  6. (canvas-line-color color)
  7. (error "line-color" "Not supported by the implementation")))
  8. (define (bg-color color)
  9. (if canvas-bg-color
  10. (canvas-bg-color color)
  11. (error "bg-color" "Not supported by the implementation")))
  12. (define (line-width width)
  13. (if canvas-line-width
  14. (canvas-line-width width)
  15. (error "line-width" "Not supported by the implementation")))
  16. (define (normalize-angle theta)
  17. (cond
  18. ((>= theta 360)
  19. (normalize-angle (- theta 360)))
  20. ((< theta 0)
  21. (normalize-angle (+ theta 360)))
  22. (else theta)))
  23. (define (xy->facing x y)
  24. (define as-degree (/ (* 180 (atan (/ (inexact y) x))) 3.1415926))
  25. (normalize-angle
  26. (if (>= x 0)
  27. as-degree
  28. (+ 180 as-degree))))
  29. (define-operation (get-pos turt))
  30. (define-operation (set-pos! turt new-pos))
  31. (define-operation (get-orient turt))
  32. (define-operation (set-orient! turt new-H new-L new-U))
  33. (define-operation (pen-down? turt))
  34. (define-operation (shown? turt))
  35. (define-operation (show! turt))
  36. (define-operation (hide! turt))
  37. (define-operation (pen-up! turt))
  38. (define-operation (pen-down! turt))
  39. (define-operation (forward! turt dist))
  40. (define-operation (yaw! turt theta))
  41. (define-operation (pitch! turt theta))
  42. (define-operation (roll! turt theta))
  43. (define-operation (nutate! turt))
  44. (define-operation (left! turt theta))
  45. (define-operation (right! turt theta))
  46. (define-operation (back! turt dist))
  47. (define (make-turtle)
  48. (define pos #(0.0 0.0 0.0))
  49. (define H #(1.0 0.0 0.0))
  50. (define L #(0.0 1.0 0.0))
  51. (define U #(0.0 0.0 1.0))
  52. (define pen-state #t)
  53. (define shown-state #f)
  54. (object
  55. ((get-pos self)
  56. pos)
  57. ((set-pos! self new-val)
  58. (set! pos new-val))
  59. ((get-orient self)
  60. (list H L U))
  61. ((set-orient! self new-H new-L new-U)
  62. (when shown-state
  63. (let ((old-tilt-x (vector-ref H 0))
  64. (old-tilt-y (vector-ref H 1))
  65. (new-tilt-x (vector-ref new-H 0))
  66. (new-tilt-y (vector-ref new-H 1)))
  67. (define old-tilt (xy->facing old-tilt-x old-tilt-y))
  68. (define new-tilt (xy->facing new-tilt-x new-tilt-y))
  69. (image-rotate (vector-ref pos 0)
  70. (vector-ref pos 1)
  71. old-tilt
  72. (- new-tilt old-tilt))))
  73. (set! H new-H)
  74. (set! L new-L)
  75. (set! U new-U))
  76. ((pen-down? self)
  77. pen-state)
  78. ((shown? self)
  79. shown-state)
  80. ((show! self)
  81. (set! shown-state #t)
  82. (when canvas-show-turtle
  83. (let ((h-x (vector-ref H 0))
  84. (h-y (vector-ref H 1)))
  85. (canvas-show-turtle (vector-ref pos 0)
  86. (vector-ref pos 1)
  87. (+ (/ (* 180 (atan (/ h-y h-x))) 3.14159265)
  88. (if (negative? h-x)
  89. 180
  90. 0))))))
  91. ((hide! self)
  92. (set! shown-state #f)
  93. (when canvas-hide-turtle
  94. (canvas-hide-turtle)))
  95. ((pen-up! self)
  96. (set! pen-state #f))
  97. ((pen-down! self)
  98. (set! pen-state #t))
  99. ((forward! self dist)
  100. ;; dist is a vector of 3 real numbers.
  101. (let ((start-pos pos))
  102. (define new-pos (add-vectors start-pos
  103. (scale-vector H dist)))
  104. (draw-turtle-line (vector-ref start-pos 0)
  105. (vector-ref start-pos 1)
  106. (vector-ref new-pos 0)
  107. (vector-ref new-pos 1)
  108. (pen-down? self)
  109. (shown? self))
  110. (set-pos! self new-pos)))
  111. ((yaw! self theta)
  112. ;; theta is a real number.
  113. (set-orient! self
  114. (rotate H L theta)
  115. (rotate L (negate-vector H) theta)
  116. U))
  117. ((pitch! self theta)
  118. (set-orient! self
  119. (rotate H U theta)
  120. L
  121. (rotate U (negate-vector H) theta)))
  122. ((roll! self theta)
  123. (set-orient! self
  124. H
  125. (rotate L U theta)
  126. (rotate U (negate-vector L) theta)))
  127. ((nutate! self)
  128. (pitch! self -47.85)
  129. (yaw! self -11.43)
  130. (roll! self 43.32))
  131. ;; These are aliases that are equivalent to (turtle simple)
  132. ;; therefore removing the need for a separate simple library
  133. ((left! self theta)
  134. (yaw! self theta))
  135. ((right! self theta)
  136. (yaw! self (- theta)))
  137. ((back! self dist)
  138. (forward! self (- dist)))))
  139. (define fd! forward!)
  140. (define bk! back!)
  141. (define lt! left!)
  142. (define rt! right!)
  143. (define pu! pen-up!)
  144. (define pd! pen-down!)