Here's a bit of setup code:(require 2htdp/image) (require 2htdp/universe) ;; Constants (define WIDTH 300) (define HEIGHT 160) (define MT (empty-scene WIDTH HEIGHT)) ;; next-size : Number -> Number (define (next-size s) (/ s 2))And here's our target image for the first function:
Note that the circles get smaller as they move to the right.
- Design the function
circlesthat takes two numbers,
size, and a Scene
If size is less-than or equal to
2, then your function returns the
scnunchanged (base case). Otherwise, your function should place a
circlein the Scene that results from a recursive call to
(+ size (next-size size)), a new size of
(next-size size), and using the original Scene.
next-sizeso that it divides by
3/2(or something a bit smaller), instead of
WIDTHso you get something like the image below.
- Now we create a similar function
spiral, that takes four numbers and a Scene. In addition to
size, the function also takes
ang, which represent the center
ycoordinate of a circle, and the current angle (in radians).
Now in your recursive call to
spiral, you must update
ybased on your expert knowledge of trigonometry:next-x = x + (size+nextsize) * cos(ang)And also add to the ang, something like
next-y = y + (size+nextsize) * sin(ang)
(/ pi 10), with a starting angle of
(/ pi -10)should give you something like the image below.
- Modify the various parameters to your function to get interesting results.
For example, if you modify the function to be structurally recursive (using
next-size), and draw the same size circles each time (be sure you terminate!), you might get something like the image below.
Now that we've refreshed your trig senses, lets do a few generative fractals. First up is a simple "tree" fractal.
Here's some helper code to get you started:;; put-line : Number Number Number Number String Scene -> Scene ;; Put a line in the scene starting at (x,y) len distance in the given direction ;; with the given color (define (put-line x y ang len color scn) (place-image (line (* (cos ang) len) (* (sin ang) len) color) (+ x (* (cos ang) (/ len 2))) (+ y (* (sin ang) (/ len 2))) scn))
- Design the function
tree, that takes four numbers,
len, and draws a tree into a given Scene.
If the length is less than 3, then the function just puts a line at x/y/ang/len of some color (say green?) into the scene.
Otherwise the function puts a brown line at x/y/ang/len, and recursivaly calls itself twice: once with x/y placed one third up the trunk at an angle off to the left, and another with x/y placed two thirds up the trunk at an angle off to the right. The length should be cut in half... altogether something like:next-x = x + len/3 * cos(ang)And the same for ang - pi/3, at 2*len/3 away.
next-y = y + len/3 * sin(ang)
next-ang = ang + pi/3
next-len = len/2
You should be able to modify the parameters to get various images such as the following... I did pi/6, and only cut the length in half.
- Similar to the tree fractal, we can do interesting things by changing recursive calls. The Koch snowflake is a rather cool recursive fractal.
Design the function
kochthat takes the same arguments as
tree, with an additional
iterparameter that tracks the number of iterations left.
For each iteration you cut the line into three pieces and make four recursive calls. The key is that you only put a line when the number of iterations are up (i.e., zero)
The images below show the first 4 iterations and a more elaborate version (bigger, with 6 iterations), of a "snowflake" variation that I think looks pretty cool. See if you can emulate it.