
// Turtle.java
// Robert Sedgewick and Kevin Wayne
// Slightly modified by Andrew Davison, Nov. 2022

/* Turtle graphics operations.

*/

import java.awt.Color;


public class Turtle
{
  private double x, y;       // turtle is at (x, y)
  private double angle;      // facing this many degrees
                             // counterclockwise from the x-axis
  private double step = 1;
  private boolean penDown = true;

  private double minX, maxX, minY, maxY;


  public Turtle()
  {
     this(0, 0, 90);
  }


  public Turtle(double x, double y, double a)
  /* start at (x, y), facing a degrees counterclockwise from the
     x-axis
  */
  {
    StdDraw.setCanvasSize(400, 400);
    StdDraw.setScale(-10, 10);

    minX = -10; maxX = 10;
    minY = -10; maxY = 10;

    setX(x);
    setY(y);
    angle = a;

  }  // end of Turtle()


  public double getX()
  {  return x; }

  public void setX(double val)
  {
    x = val;
    if (x < minX)
      minX = x;
    else if (x > maxX)
      maxX = x;
   }

  public double getY()
  {  return y; }

  public void setY(double val)
  {
    y = val;
    if (y < minY)
      minY = y;
    else if (y > maxY)
      maxY = y;
  }


  public double getAngle()
  {  return angle; }


  public void setAngle(double val)
  {  angle = val; }


  public void setStep(double val)
  {  step = val; }

  public double getStep()
  {  return step; }



  public void penDown()
  {  penDown = true;  }

  public void penUp()
  {  penDown = false;  }


  public double[] getRanges()
  {  return new double[]{ minX, maxX, minY, maxY};  }



  // rotate orientation delta degrees counterclockwise
  public void left(double delta)
  {   angle = (angle + delta)% 360;  }

  public void right(double delta)
  {   angle = (angle - delta)% 360;  }



  public void forward()
  {
    double oldx = x;
    double oldy = y;
    x += step * Math.cos(Math.toRadians(angle));
    y += step * Math.sin(Math.toRadians(angle));

    if (x < minX)
      minX = x;
    else if (x > maxX)
      maxX = x;

    if (y < minY)
      minY = y;
    else if (y > maxY)
      maxY = y;

    if (penDown)
      StdDraw.line(oldx, oldy, x, y);

  } /* forward */



  public void setPenColor(Color color)
  {  StdDraw.setPenColor(color);  }

  public Color getPenColor()
  {  return StdDraw.getPenColor();  }


  public void setPenRadius(double radius)
  {  StdDraw.setPenRadius(radius);   }

  public double getPenRadius()
  {  return StdDraw.getPenRadius();   }


  public void setCanvasSize(int width, int height)
  {  StdDraw.setCanvasSize(width, height);  }


  public void setScale(double min, double max)
  {  StdDraw.setScale(min, max);  }

  public void setXscale(double min, double max)
  {  StdDraw.setXscale(min, max);  }

  public void setYscale(double min, double max)
  {  StdDraw.setYscale(min, max);  }


}  // end of Turtle class
