Turtles do a spiral

A Diaspora* post with interesting Python code inspired me to explore Racket's turtles and to learn about radians and the law of cosines. Theodorus Spiral from DrRacket's turtles (All the italicized words in the previous sentence have endnotes).


#lang racket

(require graphics/turtles)

(turtles #t)
; http://mathcentral.uregina.ca/QQ/database/QQ.09.07/h/lucy1.html

;; make two sides of right triangle and return to start
(define (right-triangle a b)
  (define (right-angle a b)
    (draw a)
    (turn/radians (/ pi 2))
    (draw b))
   (right-angle a b)))

;; save visual noise with a short square procedure
(define (sqr n)
  (* n n))

;; get the hypotenuse from two sides of a right triangle
(define (get-hyp a b)
  (sqrt (+ (sqr a) (sqr b ))))

;; get radians for from right triangle so the next
;; spiral segment can be drawn
(define (get-rad a b)
  (define hyp (get-hyp a b))
    (- (sqr b) (+ (sqr hyp) (sqr a)))
    (- (* 2 hyp a)))))

;; After getting started the a side will be the
;; hypotenuse of the previous segment
(define (theodor-segment a b)
  (turn/radians (get-rad a b))
  (right-triangle (get-hyp a b) b))

(define (theodorus-spiral start-len stop-seq)
  (define (helper a b stop-seq)
      ((= 0 stop-seq) (theodor-segment a b))
      (#t (begin
            (theodor-segment a b)
            (helper (get-hyp a b) b (sub1 stop-seq))))))
  (helper start-len start-len stop-seq))

(theodorus-spiral 75 15)

(save-turtle-bitmap "theodorus-spiral.png" 'png)

I'm not sure how many hours it took me to do this: about more than half a day if I include the night hours. Most of the time was spent looking for the trigonometry knowledge on pages in book and on-line. And more time got spent trying generate the image with pict or draw or values-turtels or 2htdp/image. It would be nice to learn more math and have this task push me to get competent with draw... For one day it's good enough to get a better feel for radians and the law of cosines... I wonder how this sort of task-driven learning would work with a classroom of learners...


#turtles #DrRacket #trigonometry #triangles #getanglefromsidelengths