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 的两个分支之间的分离角度、旋转等)的情况下绘制 Y 的方法。这将使您的代码更具可读性和更易于理解。

至于移动到中心,只需考虑坐标平面上的 Y。根据 Y 轴的旋转及其起点,您可以计算中心点。

图表

只需将其分解为 x 和 y 分量即可。

分解成组件

有了这些信息,我们可以求解ab

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

然后将此添加到您的 x 和 y 以计算 Y 的中心点。

至于保持恒定的长度,你知道水平。在第一层,level == 1。但是下一层的长度应该是length * (2^level)。在这种情况下,长度/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 回答