package lab.turtle; import java.awt.Color; import java.util.Random; /** * A class that supports turtle graphics inside a circular area. The turtle has a * position, an angle representing the direction that the turtle is facing in, * and a pen color. The turtle stays inside the circle centered at the point * (0, 0) and having radius equal to 1. * The angle of the turtle is measured relative to the positive x-axis, and * the turtle ensures that the angle is always greater than -360 degrees and * less than +360 degrees. * * @author EECS1030_W14_15 * */ public class Turtle { // DO NOT ADD ANY OTHER NON-STATIC FIELDS TO THE CLASS /** * The position of this turtle. */ private Point2D position; /** * The direction of this turtle. */ private double angle; /** * The pen color of this turtle. */ private Color penColor; // DO NOT ADD ANY OTHER NON-STATIC FIELDS TO THE CLASS /** * Create a turtle located at the exact middle of the circle with an angle of * 0.0 degrees and a pen color of Color.BLACK. */ public Turtle() { this(new Point2D(), 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 Turtle(Turtle other) { } /** * Create a turtle with the given starting position, angle, and pen color. The * starting position must be inside or on the circle that the turtle * is confined to, 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 * @pre. c is not null * @throws IllegalArgumentException if the starting position is * not inside or on the circle that the turtle is confined to */ public Turtle(Point2D position, double angle, Color c) { } /** * 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 turtle will not move outside of the circle that it is confined to. If the * specified distance would cause the turtle to move outside of the circle, * then the turtle will move as far as possible in its current direction and * then stop at the perimeter of the circle. * * @param distance * the distance to move * @throws IllegalArgumentException * if the distance is less than zero */ public void move(double distance) { } /** * Returns the maximum distance that the turtle can move from its current * position until it hits the * perimeter of the circle that the turtle is constrained to move on. The * maximum distance is computed by considering the turtle's current position * and direction, as well as the perimeter of the circle. * * * @return the maximum distance that the turtle can move from its * current position */ public double maxDistance() { return 0; } /** * Turns the turtle to the left, increasing its angle by 90.0 degrees. The * angle of the turtle is always corrected to be greater than * -360 degrees and less than +360 degrees. * */ public void turnLeft() { } /** * Turns the turtle to the right, decreasing its angle by 90.0 degrees. The * angle of the turtle is always corrected to be greater than * -360 degrees and less than +360 degrees. */ public void turnRight() { } /** * 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 be greater than * -360 degrees and less than +360 degrees. * * @param delta * the amount by which to turn the turtle */ public void turn(double delta) { } /** * Sets the pen color. * * @param c * the new pen color * @pre. c is not null */ public void setPenColor(Color c) { } /** * Gets the current pen color. * * @return the current pen color */ public Color getPenColor() { } /** * Gets the current position of the turtle. The client cannot change the * position of the turtle using the point returned by this * method. To move the turtle the client must use move. * * @return the current position of the turtle */ public Point2D getPosition() { } /** * 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() { } /** * Returns a string representation of this turtle. The string returned * is simply the position of the turtle, followed by the angle * of the turtle, followed by the pen color of the turtle. For example, * the turtle made with the default constructor has the following * string representation: * *

(0.5, 0.5), 0.0 degrees, java.awt.Color[r=0,g=0,b=0]
* *

* The string for the pen color is identical to that produced by * invoking toString in java.awt.Color * * @return a string representation of this turtle * @see java.lang.Object#toString() */ @Override public String toString() { return this.position + ", " + this.angle + " degrees, " + this.penColor; } /** * This main method creates an infinite loop where a turtle * moves changing direction whenever it encounters the perimeter * of its enclosing circle. The pen color changes to a random * color every time the turtle changes direction. * * @param args not used */ public static void main(String[] args) { StdDraw.circle(0, 0, 1.01); Random r = new Random(System.currentTimeMillis()); Turtle t = new Turtle(); // the distance that the turtle will try to move each step final double step = 0.005; while (true) { double maxStep = t.maxDistance(); if (maxStep <= step) { // move and then turn around t.move(maxStep); double newAngle = TurtleUtil.bounceDirection(t.getPosition(), t.getAngle()); t.turn(newAngle - t.getAngle() + (r.nextDouble() - 0.5) * 10); t.setPenColor(TurtleUtil.nextColor()); t.move(step); } else { t.move(step); } } } }