Example #1
0
  /**
   * Recursive helper for move. Uses its own helper methods, overrunsTop, Bottom, Right, and Left to
   * calculate next move. Calls itself recursively when turtle hits the edge of the screen
   * (overruns).
   *
   * @param pixels changes every time with recursive call until it is less than 0 (because of
   *     possible rounding errors with trig functions)
   */
  protected void moveRecursiveHelper(double pixels) {
    // Checkstyle and clean-up rules conflict on brackets - this is a recurring problem
    // throughout our code
    if (pixels <= 0 + PRECISION_LEVEL) return;

    Location currentLocation = getLocation();
    Location nextLocation = getLocation();
    Location nextCenter = nextLocation;
    nextLocation.translate(new Vector(getHeading(), pixels));
    Location[] replacements = {nextLocation, nextCenter};

    if (nextLocation.getY() < 0) {
      // top
      replacements = overrunsTop();
    } else if (nextLocation.getY() > myCanvasBounds.getHeight()) {
      // bottom
      replacements = overrunBottom();
    } else if (nextLocation.getX() > myCanvasBounds.getWidth()) {
      // right
      replacements = overrunRight();
    } else if (nextLocation.getX() < 0) {
      // left
      replacements = overrunLeft();
    }

    nextLocation = replacements[0];
    nextCenter = replacements[1];

    setCenter(nextCenter);
    double newPixels = pixels - (Vector.distanceBetween(currentLocation, nextLocation));
    if (myPenDown) {
      myLine.addLineSegment(currentLocation, nextLocation);
    }
    moveRecursiveHelper(newPixels);
  }
Example #2
0
 /**
  * Moves turtle to location **AS IN VIEW'S COORDINATES**
  *
  * @param location to move to
  * @return distance of move
  */
 protected int setLocation(Location location) {
   double heading = getHeading();
   towards(location);
   int distance =
       (int) Vector.distanceBetween(getLocation(), convertFromViewCoordinates(location));
   move(distance);
   setHeading(heading);
   return distance;
 }