- Activity:
- Write a simple method to draw a circle.
- Add an if-statement and a recursive call to draw circle, which draws a smaller circle.
- Have students draw a picture of what they think it will look like.
- Modify the program to draw the circle toward the left.
- Modify the program to make two recursive calls. They often think that they can predict what will happen, but rarely expect the actual result.
- Below is some sample code for this assignment using the Processing Language:
void setup(){
background(0);
size(800,600);
noFill();
stroke(255);
drawCircle(400,300,200);
}//top code never changes - drawCircle evolves.
//evolution 1 - basic method that draws a circle
void drawCircle(int x, int y, int sz){
ellipse(x,y,sz,sz);
}//evolution 2 - 1st recursion - have students draw on paper what they think the code will
//produce
void drawCircle(int x, int y, int sz){
ellipse(x,y,sz,sz);
if(sz > 10)
drawCircle(x,y,sz/2);
}//evolution 3 - move the circles to the right - have students draw on paper before running code
void drawCircle(int x, int y, int sz){
ellipse(x,y,sz,sz);
if(sz > 10)
drawCircle(x+sz/2,y,sz/2);
}//evolution 4 - add second recursion - this is where the surprise happens
void drawCircle(int x, int y, int sz){
ellipse(x,y,sz,sz);
if(sz > 10) {
drawCircle(x+sz/2,y,sz/2);
drawCircle(x-sz/2,y,sz/2);
}
}