2011-02-15 54 views
0

所以在我的編程課,我們正在學習使用繪圖類。基本上畫一條線和東西,我們在課上做了一條y=mx+b線。對數螺旋

我想跳起來,開始做更瘋狂的數學!

雖然我在普林斯頓大學的網站上找到了,但我仍然遇到了麻煩。

public class Spiral { 

    public static void main(String[] args) { 
     int N   = Integer.parseInt(args[0]);  // # sides if decay = 1.0 
     double decay = Double.parseDouble(args[1]); // decay factor 

     double angle = 360.0/N; 
     double step = Math.sin(Math.toRadians(angle/2.0)); 
     Turtle turtle = new Turtle(0.5, 0.0, angle/2.0); 
     for (int i = 0; i < 10*N; i++) { 
      step /= decay; 
      turtle.goForward(step); 
      turtle.turnLeft(angle); 
     } 

    } 
} 

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 

    // start at (x0, y0), facing a0 degrees counterclockwise from the x-axis 
    public Turtle(double x0, double y0, double a0) { 
     x = x0; 
     y = y0; 
     angle = a0; 
    } 

    // rotate orientation delta degrees counterclockwise 
    public void turnLeft(double delta) { 
     angle += delta; 
    } 

    // move forward the given amount, with the pen down 
    public void goForward(double step) { 
     double oldx = x; 
     double oldy = y; 
     x += step * Math.cos(Math.toRadians(angle)); 
     y += step * Math.sin(Math.toRadians(angle)); 
     StdDraw.line(oldx, oldy, x, y); 
    } 

    // pause t milliseconds 
    public void pause(int t) { 
     StdDraw.show(t); 
    } 


    public void setPenColor(Color color) { 
     StdDraw.setPenColor(color); 
    } 

    public void setPenRadius(double radius) { 
     StdDraw.setPenRadius(radius); 
    } 

    public void setCanvasSize(int width, int height) { 
     StdDraw.setCanvasSize(width, height); 
    } 

    public void setXscale(double min, double max) { 
     StdDraw.setXscale(min, max); 
    } 

    public void setYscale(double min, double max) { 
     StdDraw.setYscale(min, max); 
    } 


    // sample client for testing 
    public static void main(String[] args) { 
     double x0 = 0.5; 
     double y0 = 0.0; 
     double a0 = 60.0; 
     double step = Math.sqrt(3)/2; 
     Turtle turtle = new Turtle(x0, y0, a0); 
     turtle.goForward(step); 
     turtle.turnLeft(120.0); 
     turtle.goForward(step); 
     turtle.turnLeft(120.0); 
     turtle.goForward(step); 
     turtle.turnLeft(120.0); 
    } 

} 

龜使用這個類:StdDraw這只是編碼方式太多了線我在這裏貼上。

我不斷收到一個錯誤,當我去執行螺旋:

java.lang.ArrayIndexOutOfBoundsException: 0 
    at Spiral.main(Spiral.java:4) 

不知道爲什麼。任何人都可以幫助我,讓我可以玩這個嗎?

回答

2

您是否指定了兩個命令行參數?它看起來像是需要步驟數和衰減作爲參數,並且如果不指定這些參數將會崩潰。

例如:

java Spiral 10 1.1

+0

我做過嘗試,但它給了我同樣的錯誤不管是什麼。你使用了什麼值? 我也在BlueJ上運行這個。 – 2011-02-15 06:11:29