Wednesday, 21 November 2012

Week 11: The Snowflake

It's all about recursion this week.
The rkt. files spiral and koch are about doing the same thing.

For koch, it is a function to make the 1/3 of a snowflake.
Here's the function,

(define (koch d)
  (cond
    [(zero? d) (line 5 0 "blue")]
    [else
     (beside/align
      "bottom"
      (koch (sub1 d))
      (rotate 60 (koch (sub1 d)))
      (rotate -60 (koch (sub1 d)))
      (koch (sub1 d)))]))
These are two of the outcomes,

(koch 0)
_
As d=0, when (zero? 0), a blue line (5) shows up.

(koch 1)
_/\_
As d=1, it goes to [else ...], when it goes to (koch (sub1 1)), it goes back to (koch 0), which is a blue line.


After "koch", it's followed by "koch-snowflake"
Here's the function,


(define (koch-snowflake d)
  (above
   (beside
    (rotate 60 (koch d))
    (rotate -60 (koch d)))
   (rotate 180 (koch d))))


It forms a complete snowflake by putting three "koches" (three 1/3 of a snowflake) together like a triangle.
It is fun when I trying to imagine the output by myself before checking it with DrRacket.


No comments:

Post a Comment