Week 12

  • Recursion

  • Base case and rule

1. Resources:

2. factorial

// iterative version
int factorial(int n) {
  int product = 1;
  for (int i = n; i > 1; i--) {
    product = product * i;
  }
  return product;
}

int factorialR(int n) {
  if (n <= 1) { // base case
    return 1;
  }
  int product = n * factorialR(n-1);
  return product;
}

void setup () {
  int result1 = factorial(3); // 3 * 2 * 1
  println(result1);
  println(factorial(4)); // 4 * 3 * 2 * 1
  println(factorial(0));

  println(factorialR(3));
  println(factorialR(4)); // 4 * 3 * 2 * 1
  println(factorialR(0));
}

3. snowflake

void snowflake(float x, float y, float len, float angle) {
  if (len < 10) { // base case
    noStroke();
    fill(#C5FAF9, 100);
    ellipse(x, y, len, len);
    return;
  }
  float endx = x + len * cos(angle);
  float endy = y + len * sin(angle);
  stroke(#C5FAF9, 100);
  strokeWeight(8);
  line(x, y, endx, endy);
  stroke(255);
  strokeWeight(1);
  line(x, y, endx, endy);
  float lowx = x + 0.25 * len * cos(angle);
  float lowy = y + 0.25 * len * sin(angle);
  snowflake(lowx, lowy, 0.5 * len, angle + PI/3);
  snowflake(lowx, lowy, 0.5 * len, angle - PI/3);
  float hix = x + 0.65 * len * cos(angle);
  float hiy = y + 0.65 * len * sin(angle);
  snowflake(hix, hiy, 0.3 * len, angle + PI/6);
  snowflake(hix, hiy, 0.3 * len, angle - PI/6);
}
void setup() {
  size(500, 500);
  background(#141B8E);
  int num = 8;
  for (int i = 0; i < num; i++) {
    float angle = 2 * PI * i / num;
    snowflake(width * 0.5, height * 0.5, 200, angle);
  }
}

4. printList

void printList(int[] L, int start) {
  for (int i = start; i < L.length; i++) {
    println(L[i]);
  }
}

void printListR1(int[] L, int start) {
  // base cases
  if (L.length == 0) {
    return;
  }

  if (start > L.length-1) {
    return;
  }

  println(L[start]);
  printListR1(L, start+1);
}

void printListR2(int[] L) {
  // base cases
  if (L.length == 0) {
    return;
  }
  println(L[0]);
  printListR2(subset(L,1));
}

void setup() {

  int[] L = {0, 2, 4};

  printList(L, 0);
  printListR1(L, 0);
  printListR2(L); // a different approach using sub-lists
}

5. sumList

// iterative function that sum all the elements
// starting at index
int sumList(int[] L, int start) {
  int sum = 0;
  for (int i = start; i < L.length; i++) {
    sum += L[i];
  }
  return sum; // placeholder
}

int sumListR(int[] L, int start) {
  if (L.length == 0) {
    return 0;
  }

  if (start > L.length-1) {
    return 0;
  }

  int sum = L[start] + sumListR(L, start+1);
  return sum;
}

void setup() {
  int[] L = {-1, -2, 2, 1, 3};
  println(sumList(L, 0));
  println(sumListR(L, 0));
}

6. tower

void tower(float x, float y, float w, float h, int N) {
   if (N <= 0) {
     return;
   }

   float a = x-w/2;
   float b = y-h;
   rect(a, b, w, h);
   tower(x, y-h, w*0.8, h*0.8, N-1);
}

void setup() {
  size(500,500);
  tower(width*0.5, 450, 200, 130, 5);
}

7. sierpinski triangle

void sierpinski(float x, float y, float h, int N) {

  if (N <= 0) {
    return;
  }

  triangle(x-h/2, y, x+h/2, y, x, y+h);
  sierpinski(x-h/4, y, h/2, N-1);
  sierpinski(x+h/4, y, h/2, N-1);
  sierpinski(x, y+h/2, h/2, N-1);

}

void setup() {
  size(500,500);
  sierpinski(width*0.5, 0, height, 11);
}