2013-10-04 9 views
5

私は再帰的に再帰的に深度を定義する引数 "n"を持つ行を描画するプログラムを終了しました。私は2つの機能を持っています.1つは相対的に左の線を描画し、もう1つは比較的右の線を描画します。私はそれを最初の4つのレベルで動作するように見えるが、その後、線が正確に表現するには小さすぎるか、線の間の区切りが恣意的に見えるので、私のコードに何か問題がある。誰かが自分のコードをテストして問題の原因を見つけることができるかどうかを確かめることを望んでいました。プログラムのデバッグを確認してください

次の画像は、EDIT

深さ10です:

public class Art 
{ 

//draws the relatively left line 
public static void drawLeftLine(double x0, double y0, double x1, double y1) 
{ 
    //define new x coordinate for line 
    //double x2 = (1/3.0)*(x1 - x0); 

    //color of line 
    StdDraw.setPenColor(StdDraw.BLUE); 


    //draw line by adding new x coord to original 
    StdDraw.line(x0, y0, x1, y1); 

} 
//draw relatively right line 
public static void drawRightLine(double x0, double y0, double x1, double y1) 
{ 
    //define new x coord for line 
    //double x2 = (2/3.0)*(x1 - x0); 

    //color of line 
    StdDraw.setPenColor(StdDraw.BLUE); 


    //draw line by adding new x coord to original 
    StdDraw.line(x0, y0, x1, y1); 

} 

public static void cantor(int n, double x0, double y0, double x1, double y1) 
{ 
    if (n == 0) 
     return; 

    drawLeftLine(x0, y0, x1, y1); 
    drawRightLine(x0, y0, x1, y1); 

    y0 = y0 - 0.1; 
    y1 = y1 - 0.1; 



    cantor(n-1, x0, y0, x0 + ((x1 - x0))/3.0, y1); //left 
    cantor(n-1, (2.0/ 3) * (x1 - x0) + x0, y0, x1, y1); //right 

} 

public static void main(String[] args) 
{ 
    //change n into integer (depth) 
    int n = Integer.parseInt(args[0]); 

    //specify inital values for line 
    double x0 = 0; 
    double y0 = 0.9; 
    double x1 = 0.9; 
    double y1 = 0.9; 



    //recursive function cantor 
    cantor(n, x0, y0, x1, y1); 

} 
} 

答えて

4

は私がのために描画が不正に見えると思いますけれども、コードの一部を固定し、まだ助けが必要素敵な倍精度値のすべてが線分間の望ましくない重なりを引き起こす離散ピクセルで近似されているという事実(下のEDITを参照)。しかし、あなたのコードについてのいくつかのコメントは:

1)drawLeftLinedrawRightLineのメソッドは、現在全く同じことを描いているので、必要ありません。各ステップで、cantorを2回(削除された内部3分の1に1回)呼び出すので、描画する必要がある各線分に対してcantorを1回呼び出します。このように、私はすべての図を直接cantorメソッドに入れます。

2)y0y1は常に同じですから、変数を1つだけy個に減らします。

3)私が代わりに0.1によって毎回y値をデクリメントの、あなたが決定したグローバル変数を持つ必要があります)

double third = (x1 - x0)/3; 
cantor(n - 1, x0, x0 + third, y); // left 
cantor(n - 1, x1 - third, x1, y); // right 

4までの新しいx0x1値を計算するために数学を単純化しますこの量を減らす必要があります(それ以外の場合はn > 10が壊れます)。この値は1.0/nに設定することができます。

5)塗装するたびにペンの色を設定する必要はありません。メインメソッドで一度だけ設定できます。

6)StdDrawは描画中の画像の周囲に境界線を設定しているため、座標を0.9から開始する必要はありません。代わりに1を使用できます。

private static double yIncrement; 

public static void cantor(int n, double x0, double x1, double y) { 
    if (n == 0) 
    return; 

    StdDraw.line(x0, y, x1, y); 

    y = y - yIncrement; 

    double third = (x1 - x0)/3; 
    cantor(n - 1, x0, x0 + third, y); // left 
    cantor(n - 1, x1 - third, x1, y); // right 

} 

public static void main(String[] args) { 
    //change n into integer (depth) 
    int n = Integer.parseInt(args[0]); 

    // specify inital values for line 
    double x0 = 0; 
    double x1 = 1; 
    double y = 1; 

    yIncrement = 1.0/n; 
    StdDraw.setPenColor(Color.BLUE); 

    // recursive function cantor 
    cantor(n, x0, x1, y); 
} 

EDIT:丸めモードStdDrawキャンバスサイズ、キャンバスの拡大縮小の設定、および線分の終点で遊んで、あなたがわずかに良い画像を得ることができます(コードは次のようになりますこれらの提案に続き

以下のコードは、3^9画素(19Kピクセル)の幅を必要とする絶対的な正しさを持つ第十レベルまでのすべてを表示する)

private static double yIncrement; 

public static void cantor(int n, double x0, double x1, double y) { 
    if (n == 0) 
    return; 

    x0 = Math.ceil(x0); 
    x1 = Math.floor(x1); 

    StdDraw.line(x0, y, x1, y); 

    y = y - yIncrement; 

    double third = (x1 - x0)/3; 
    cantor(n - 1, x0, x0 + third, y); // left 
    cantor(n - 1, x1 - third, x1, y); // right 

} 

public static void main(String[] args) { 
    // change n into integer (depth) 
    int n = Integer.parseInt(args[0]); 

    int width = 1920; 
    int height = 1080; 

    StdDraw.setCanvasSize(width, height); 

    // specify inital values for line 
    double x0 = 0; 
    double x1 = width; 
    double y = 1; 

    yIncrement = 1.0/n; 
    StdDraw.setPenColor(Color.BLUE); 
    StdDraw.setXscale(0, width); 

    // recursive function cantor 
    cantor(n, x0, x1, y); 
} 

を8レベルまでほとんどが正しく見える画像を生成します。レベル9では3^8 = 6Kです。レベル8の場合は3^7 = 2kです。そのため、1.9Kのピクセル幅と整数の丸めでほぼ正確に見えます。

+0

私は実際にこのプロジェクトのキャンバスサイズを混乱させることは許されませんが、私は数分でこれを実装しようとしています。それは素晴らしいと、おそらくグラフィカルなエラーであることを聞いて素晴らしいです。あなたは男です!ありがとうございました! – user2782981

関連する問題