0
import gpdraw.*;

public class Y2K {

// Attributes
SketchPad pad;
DrawingTool pen;

// Constructor
public Y2K() {

    pad = new SketchPad(600, 600, 50);
    pen = new DrawingTool(pad);

    // Back the pen up so the Y is drawn in the middle of the screen
    pen.up();
    pen.setDirection(270);
    pen.forward(150);
    pen.down();
    pen.setDirection(90);
}

public void drawY(int level, double length) {

    // Base case:  Draw an Y
    if (level == 0) {

        //pen.setDirection(90);
        pen.forward(length);
        pen.turnRight(60);
        pen.forward(length);
        pen.backward(length);
        pen.turnLeft(120);
        pen.forward(length);
        pen.backward(length);
    }


    // Recursive case:  Draw an L at each midpoint
    // of the current L's segments
    else {


            //Drawing the bottom "leg" of our Y shape
            pen.forward(length / 2);
            double xpos1 = pen.getXPos();
            double ypos1 = pen.getYPos();
            double direction1 = pen.getDirection();


            pen.turnRight(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            double xpos2 = pen.getXPos();
            double ypos2 = pen.getYPos();
            double direction2 = pen.getDirection();

            //Drawing upper Right Leg
            pen.turnRight(60);
            pen.forward(length / 2); //going to the midpoint
            double xpos3 = pen.getXPos();
            double ypos3 = pen.getYPos();
            double direction3 = pen.getDirection();
            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos3, ypos3);
            pen.setDirection(direction3);
            pen.down();
            pen.forward(length / 2);

            //drawing upper left leg
            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            pen.turnLeft(60);
            pen.forward(length / 2);
            double xpos4 = pen.getXPos();
            double ypos4 = pen.getYPos();
            double direction4 = pen.getDirection();

            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos4, ypos4);
            pen.setDirection(direction4);
            pen.down();
            pen.forward(length / 2);
            pen.forward(length / 2);
        }

}

public static void main(String[] args) {

    Y2K fractal = new Y2K();

    // Draw Y with given level and side length
    fractal.drawY(8, 200);
}   


}

出力:

三角形の特定の脚が長すぎるため、出力がわずかにオフになります。コードが (長さ/2) 行き過ぎたからでしょうか? これをデバッグしましょう。

それ以外の場合は完全に問題なく、再帰は素晴らしく、まさに私がやりたかったことです ここに画像の説明を入力

4

1 に答える 1

2

常に Y を描画しているので、特定のパラメーター (長さ、Y の 2 つのブランチ間の分離角度、回転など) を指定して Y を描画するメソッドを作成することをお勧めします。これにより、コードが読みやすくなり、理解しやすくなります。

中心への移動については、座標平面上の Y を考えてください。Y の回転とその始点に基づいて、中心点を計算できます。

ダイアグラム

x 成分と y 成分に分解するだけです。

コンポーネントに分解

この情報があれば、 abについて解くことができます。

a = length * sin(θ)
b = length * cos(θ)

次に、これを x と y に追加して、Y の中心点を計算します。

一定の長さを保つことに関しては、あなたはレベルを知っています。最初のレベルでは、レベル == 1 です。ただし、この次のレベルの長さは、長さ * (2^レベル) である必要があります。この場合、長さ/2 (長さは -1 になるため)。

疑似コードで言えば:

public void drawY(int level, double length)
{
    //Drawing the bottom "leg" of our Y shape
    Move Forward length/2
    Save our position 
    Save our direction

    Turn to the right 90 degrees
    Recursion (call drawY())

    revert to original location
    revert to original direction
    move forward length/2 (to go to center point of Y)

    save our new position
    save our new direction 

    //Drawing upper Right Leg
    Turn 60 to the right
    Move Forward length/2 //going to the midpoint
    save our new position (don't forget the center point)
    save our new direction (don't forget the center point direction)
    Turn 90 to the left
    Recursion (call drawY())

    return to our saved position (not center one)
    return to our saved direction (not center one)

    move forward length/2

    //drawing upper left leg
    return to center point
    return to center direction

    turn left 60 
    move forward length/2
    save position (you can overwrite the center one now
    save direction (you can overwrite)

    turn left 90
    Recursion (call drawY())

    return to position
    return to direction
    move forward length/2
}
于 2012-10-09T00:45:06.513 に答える