isefire icon

TurtleExtended

isefire | PRO | 07/28/14 03:40:17 AM UTC | 0 ⭐ | 431 👁️ | Never ⏰ | []
Java |

1.04 KB

|

None

|

0 👍

/

0 👎

/* Turtle
 * <Description>
 * Draws pentagrams
 * Draws all stars with N points for all odd N
 * <Usage>
 * javac Turtle.java StdDraw.java
 * <References>
 * StdLib StdDraw
 * UAB CIS201L-Summer 2014 Dr. Sloan
 * @author
 * Dan Latham
 * <[email protected]>
 * @version 0.0.1
 *
 */
 
public class Turtle
{
    private double x, y;
    private double angle;
 
    public Turtle(double x0, double y0, double a0)
    {
        x = x0;
        y = y0;
        angle = a0;
    }
    public void turnLeft(double delta)
    {
        angle+= delta;
    }
    public void goForward(double step)
    { //Compute new position, move and draw line to it.
        double oldx = x, oldy = y;
        x += step * Math.cos(Math.toRadians(angle));
        y += step * Math.sin(Math.toRadians(angle));
        StdDraw.line(oldx, oldy, x, y);
    }
 
    public static void main(String[] args)
    { // Draw an N-gon
        int N = Integer.parseInt(args[0]);
        Turtle turtle = new Turtle(1/N+.2,1/N+.1, 360.0/N);
        for (int i = 0; i < N; i++)
        {
            double step = Math.sin(Math.toRadians(360.0/N));
            turtle.goForward(step);
            turtle.turnLeft(180.0-(180.0/N));
        }
    }
}

Comments