gpt4 book ai didi

java - 如何保存变量的值并防止它们在递归时改变?还有递归协助?

转载 作者:行者123 更新时间:2023-12-02 07:32:01 25 4
gpt4 key购买 nike

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)太远了?让我们调试一下。

否则完全没问题,递归很棒,这正是我想做的 enter image description here

最佳答案

当您不断绘制 Y 时,我建议您创建一个在给定某些参数(例如长度、Y 的两个分支之间的分离角度、旋转等)的情况下绘制 Y 的方法。这将使您的代码更具可读性和更容易理解。

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

Diagram

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

Broken up into components

根据这些信息,我们可以求解 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
}

关于java - 如何保存变量的值并防止它们在递归时改变?还有递归协助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com