CS1050, Spring 2006, Project 6
Recursion
by Jacob Bolton
Email:
Submitted: March 5, 2006
p - pause/play the animation
s - starts a new game with the computer first
This is the recursive function:
/* My recursive drawing function / void ellipsePattern(int x, int y, int r, int d) { //set the color stroke(color((x * angle) % 256, (y * angle) % 256, (r * angle) % 256)); int a = angle; if(d % 2 == 1){ //select rotation direction based on recursion level a = 360 - a; } //calculate rotated positions int dx = (int)(cos(radians(a)) * (float)(r/2)); int dy = (int)(sin(radians(a)) * (float)(r/2)); //draw ellipses ellipse(x,y,dx*2,r); ellipse(x,y,r,dy*2); //stop recursing after LEVELS levels if(d >= LEVELS){ return; } //four recursive calls ellipsePattern(x + dx, y + dy, r/2, d + 1); ellipsePattern(x - dx, y - dy, r/2, d + 1); ellipsePattern(x + dy, y - dx, r/2, d + 1); ellipsePattern(x - dy, y + dx, r/2, d + 1); }It produces frames like these:
This program would be very difficult to implement without recursion because that soulution would require individually keeping track of position and state info for each of the circles. With recursion it's much easier because all of the other elements are relative to one using a uniform rule.
Source code: P5
Built with Processing