CS111 Lab 6 Solutions -- BoomerangWorld

Understanding the pattern

How is the pattern drawn?

Given a size, number, and offset we draw number boomerangs which are size long on each side, spaced offset distance apart.

What is the base case?

If the number of boomerangs is less than 1, do nothing.

How are consecutive calls to the pattern different?

throwBoomerangs(2) = one boomerang + throwBoomerangs(1)
throwBoomerangs(3) = one boomerang + throwBoomerangs(2)
throwBoomerangs(4) = one boomerang + throwBoomerangs(3)

How do we move into position to draw the next pattern call?

We move offset distance forward (assuming that we start facing the right).

How do we meet the invariant, if any?

Invariant: We need to start and end the pattern in the same place.
We need to move offset distance backward after drawing the additional pattern.

Writing code

Writing the main recursive pattern method: throwBoomerangs()

We assume we have methods (defined below) which answer each of the questions above.
public void throwBoomerangs (double size, int number, double offset) {
  System.out.println("throwBoomerangs("+size+", "+number+", "+offset+");");
  if (number>0) {                              // base case
    boomerang(size);                           // draw the extra boomerang
    moveToNextBoomerang(offset);               // move in position to draw next pattern
    throwBoomerangs(size, number-1, offset);   // draw next pattern
    returnToStart(offset);                     // meet invariant: start and end in same place
  }
}

Writing a support method (extra design for each level): boomerang()

// draws a boomerang of specified size with inner angle 120 degrees
public void boomerang (double size) {
  System.out.println("boomerang");
  rt(60);
  fd(size);
  rt(60);
  fd(size);
  rt(150);
  pu();
  fd(size * Math.sqrt(3));
  pd();
  rt(90);
}

Writing a support method (move to position for next level): moveToNextBoomerang()

public void moveToNextBoomerang (double offset) {
  System.out.println("moveToNextBoomerang");
  pu();
  fd(offset);
  pd();
}

Writing a support method (meet invariant): returnToStart()

public void returnToStart (double offset) {
  System.out.println("returnToStart");
  pu();
  bd(offset);
  pd();
}