isefire

TurtleExtended

Jul 27th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. /* Turtle
  2.  * <Description>
  3.  * Draws pentagrams
  4.  * Draws all stars with N points for all odd N
  5.  * <Usage>
  6.  * javac Turtle.java StdDraw.java
  7.  * <References>
  8.  * StdLib StdDraw
  9.  * UAB CIS201L-Summer 2014 Dr. Sloan
  10.  * @author
  11.  * Dan Latham
  12.  * @version 0.0.1
  13.  *
  14.  */
  15.  
  16. public class Turtle
  17. {
  18.     private double x, y;
  19.     private double angle;
  20.  
  21.     public Turtle(double x0, double y0, double a0)
  22.     {
  23.         x = x0;
  24.         y = y0;
  25.         angle = a0;
  26.     }
  27.     public void turnLeft(double delta)
  28.     {
  29.         angle+= delta;
  30.     }
  31.     public void goForward(double step)
  32.     { //Compute new position, move and draw line to it.
  33.         double oldx = x, oldy = y;
  34.         x += step * Math.cos(Math.toRadians(angle));
  35.         y += step * Math.sin(Math.toRadians(angle));
  36.         StdDraw.line(oldx, oldy, x, y);
  37.     }
  38.  
  39.     public static void main(String[] args)
  40.     { // Draw an N-gon
  41.         int N = Integer.parseInt(args[0]);
  42.         Turtle turtle = new Turtle(1/N+.2,1/N+.1, 360.0/N);
  43.         for (int i = 0; i < N; i++)
  44.         {
  45.             double step = Math.sin(Math.toRadians(360.0/N));
  46.             turtle.goForward(step);
  47.             turtle.turnLeft(180.0-(180.0/N));
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment