In this lab, we’ll draw more pictures and fractals, similar to what we’ve seen in class.
Important Note Whenever we say to "design a function", we mean that you need to follow (or adapt) the design recipe. If your function is generative recursive, you must provide a termination argument.
We want to begin by drawing some circles and before we start, here is 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))
This is our first target image. Note that the circles get smaller as they move to the right.
Exercise 1 Design the function circles that takes two numbers, x and size, and an image scn. If size is less-than or equal to 2, then your function returns the scn unchanged (base case). Otherwise, your function should place a circle in the image that results from a recursive call to circles with x shifted by (+ size (next-size size)), a new size of (next-size size), and using the original image.
Exercise 2 Adjust next-size so that it divides by 3/2 (or something a bit smaller), instead of 2. Modify WIDTH so you get something like the image below.
Exercise 3 Now we create a similar function spiral that takes four numbers and an image. In addition to x and size, the function also takes y and ang, which represent the center y coordinate of a circle, and the current angle (in radians).
Hint In your recursive call to spiral, you must update x and y based on your expert knowledge of trigonometry:
next-x = x + (size + next-size(size)) * cos(ang)
next-y = y + (size + next-size(size)) * sin(ang)
next-angle = ang + pi/8
Exercise 4 Modify the various parameters to your function to get interesting results. For example, if you modify the function to be structurally recursive (using sub1 instead of 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 Image -> Image ; Put a line in the image 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))
Exercise 5 Design the function tree that takes four numbers, x, y, ang and len and draws a tree into a given image. 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 image using put-line.
Otherwise the function puts a brown line at x/y/ang/len, also using put-line and recursively 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.
Hint Altogether it should be something like:
next-x = x + len/3 * cos(ang)
next-y = y + len/3 * sin(ang)
next-ang = ang + pi/3
next-len = len/2
And the same for ang - pi/3, at 2*len/3 away.
Modify the parameters to get various images: for example, update the angle by a smaller amount or cut the length by a factor other than 1/2.
Let’s try another fractal.
Exercise 6 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 koch that takes the same arguments as tree, with an additional iter parameter 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 onto the image 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. Try emulating it, or making your own fun variation.
For more info on this fractal, you can read about it on Wikipedia.