Learning recursion through visual patterns (ii)

Visual algorithms are a good way of learning recursion. As we have seen with recursive squares, simple is often better. What about circles? We could create a program in Processing to embed a circle within a circle, each time reducing the size of the circle. Here is the setup:

float dx, dy;
void setup() {
   size(400, 400);
   noLoop();
}

void draw() {
   dx = width/2,0;
   dy = height/2.0;
   background(255);
   stroke(1);
   recCircle(200);
}

The call to the recursive function recCircle(200) uses a circle diameter of 200 pixels . A circle is then drawn at position (dx,dy) with a diameter of diam. At each recursive call, the radius is reduced to 75% its previous size.  The recursion stops when diam becomes less than 5. Here is the recursive function:

void recCircle(float diam) {
  if (diam > 5) {
    circle(dx,dy,diam);
    recCircle(diam*0.75);
  }
}

 

Here is the output:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.