package cse1030;
import princeton.introcs.StdDraw;
import java.awt.Color;
/**
* A class that supports turtle graphics.
*
* @author CSE1030Z
*
*/
public class Turtle2 {
private Point2D position;
private double angle;
private Color penColor;
/**
* Create a turtle at location (0.5, 0.5)
with an angle of
* 0.0
degrees and a pen color of Color.BLACK
.
*/
public Turtle2() {
this(new Point2D(0.5, 0.5), 0.0, Color.BLACK);
}
/**
* Create a turtle from another turtle. The created turtle has the same
* position, angle, and pen color as the other turtle.
*
* @param other
* the turtle to copy
*/
public Turtle2(Turtle2 other) {
this(other.getPosition(), other.getAngle(), other.getPenColor());
}
/**
* Create a turtle at location position
with an angle of
* 0.0
degrees and a pen color of Color.BLACK
. The
* starting position must be inside the square with corners
* (0.0, 0.0)
and (1.0, 1.0)
, otherwise an
* IllegalArgumentException
will be thrown.
*
* @param position
* the starting position of the turtle
* @throws IllegalArgumentException
if the starting position is
* not in the square with corners (0.0, 0.0)
and
* (1.0, 1.0)
*/
public Turtle2(Point2D position) {
this(position, 0.0, Color.BLACK);
}
/**
* Create a turtle at location position
with an angle of
* angle
degrees and a pen color of Color.BLACK
. The
* starting position must be inside the square with corners
* (0.0, 0.0)
and (1.0, 1.0)
, otherwise an
* IllegalArgumentException
will be thrown.
*
* @param position
* the starting position of the turtle
* @param angle
* the angle in degrees from the x axis that the turtle is facing in
* @throws IllegalArgumentException
if the starting position is
* not in the square with corners (0.0, 0.0)
and
* (1.0, 1.0)
*/
public Turtle2(Point2D position, double angle) {
this(position, angle, Color.BLACK);
}
/**
* Create a turtle with the given starting position, angle, and pen color. The
* starting position must be inside the square with corners
* (0.0, 0.0)
and (1.0, 1.0)
, otherwise an
* IllegalArgumentException
will be thrown.
*
* @param position
* the starting position of the turtle
* @param angle
* the angle in degrees from the x axis that the turtle is facing in
* @param c
* the pen color
* @throws IllegalArgumentException
if the starting position is
* not in the square with corners (0.0, 0.0)
and
* (1.0, 1.0)
*/
public Turtle2(Point2D position, double angle, Color c) {
double x = position.getX();
double y = position.getY();
if (x >= 0. && x <= 1.0 && y >= 0. && y <= 1.0) {
this.position = new Point2D(x, y);
this.angle = angle;
this.penColor = c;
} else {
throw new IllegalArgumentException("Invalid starting position!");
}
}
/**
* Moves the turtle by a given distance in the direction the turtle is
* currently facing. A line is drawn as the turtle moves to the new position.
* The distance can be negative, in which case the turtle moves backwards in
* the direction opposite to the direction it is currently facing in.
*
* @param distance
* the distance to move
*/
public void move(double distance) {
Point2D current = new Point2D(this.position);
Vector2D delta = Vector2D.dirVector(this.angle);
delta.multiply(distance);
this.position.add(delta);
StdDraw.setPenColor(this.getPenColor());
StdDraw.line(current.getX(), current.getY(), this.position.getX(),
this.position.getY());
}
/**
* Turns the turtle to the left, increasing its angle by 90.0 degrees.
*/
public void turnLeft() {
this.turn(90.0);
}
/**
* Turns the turtle to the right, decreasing its angle by 90.0 degrees.
*/
public void turnRight() {
this.turn(-90.0);
}
/**
* Turns the turtle by the specified amount in degrees. A positive
* delta
turns the turtle to the left (counterclockwise) and the
* negative delta
turns the turtle to the right (clockwise). The
* angle of the turtle is always corrected to lie within the range of
* -360
degrees and +360
degrees.
*
* @param delta
* the amount by which to turn the turtle
*/
public void turn(double delta) {
this.angle += delta;
this.angle %= 360.0;
}
/**
* Sets the pen color.
*
* @param c
* the new pen color
*/
public void setPenColor(Color c) {
this.penColor = c;
}
/**
* Gets the current pen color.
*
* @return the current pen color
*/
public Color getPenColor() {
return this.penColor;
}
/**
* Gets the current position of the turtle. The client cannot change the
* position of the turtle using the Point2D
returned by this
* method. To move the turtle the client must use move
.
*
* @return the current position of the turtle
*/
public Point2D getPosition() {
return new Point2D(this.position);
}
/**
* Gets the direction that the turtle is facing in as an angle measured from
* the x axis. The angle of the turtle is always in the range of
* -360
degrees and +360
degrees.
*
* @return the angle measured in degrees from the x axis that the turtle is
* facing
*/
public double getAngle() {
return this.angle;
}
}