For this assignment I modified the Recursion example, as shown below:
/**
* Recursion.
*
* A demonstration of recursion, which means functions call themselves.
* Notice how the drawCircle() function calls itself at the end of its block.
* It continues to do this until the variable "level" is equal to 1.
*/
void setup() {
size(640, 360);
noStroke();
noLoop();
}
void draw() {
drawCircle(width/2, 280, 6);
}
void drawCircle(int x, int radius, int level) {
float tt = 126 * level/4.0;
fill(tt);
ellipse(x, height/2, radius*2, radius*2);
if(level > 1) {
level = level - 1;
drawCircle(x - radius/2, radius/2, level);
drawCircle(x + radius/2, radius/2, level);
}
}
I liked the idea of recursion but I wanted to add some dynamic elements: Here's my final code:
/**
* Pulsating Recursion.
*
*/
int rspeed = 2;
int xspeed = 1;
int r;
int x;
int y = height/2;
void setup() {
size(640, 360);
noStroke();
}
void draw() {
background(130);
rspeed = changeSpeed(0,150,r, rspeed);
xspeed = changeSpeed(-200,200,x,xspeed);
r = r+rspeed;
x = x+xspeed;
drawCircle(width/2+x, r, 10,y);
}
int changeSpeed(int min, int max, int input, int inputSpeed){
if(input > max || input < min){
inputSpeed = inputSpeed*-1;
}
return inputSpeed;
}
void drawCircle(int x, float radius, int level,int y) {
float tt = 60 * level/4.0;
fill(tt);
ellipse(x, y+120, radius*2, radius*2);
if(level > 1) {
level = level - 1;
drawCircle(x, radius/1.2, level,y);
}
}
To do this, I added a loop that incremented the radius and x-position of the recursive circles. To increment these values I created a function called changeSpeed that makes it easy to modify the dynamics of the pulsating circles. While the initial recursive pattern was cool, I modified it to be more simple, so that it acted like a pulse. To do this, I made all the circles concentric and added several more layers of recursion.
No comments:
Post a Comment