我想画
我使用递归完美地完成了
private void paintTree2(int order, int originX, int originY, int length, double angle) {
if (order == -1) {
return;
}
System.out.println("branch2 -> x:" + originX + " y:" + originY + " lenth:" + length + " angle:" + angle);
Point endPoints = calculatePoint(originX, originY, length, angle); //Get End point of branch for line to draw
Line line = new Line(originX, originY, endPoints.x, endPoints.y); //line object representing the branch
line.setStroke(Color.BLACK); //color set to branch
line.setStrokeWidth(1); //branch color width
drawingPane.getChildren().add(line); // add branch
paintTree2(order - 1, endPoints.x, endPoints.y, length / 2, angle - 35); //recursion to draw right branch
paintTree2(order - 1, endPoints.x, endPoints.y, length / 2, angle + 35); //recursion to draw left branch
}
但是当我尝试使用堆栈和循环时,我得到
private void paintTree(int order, int originX, int originY, int length, double angle) {
Stack s = new Stack();
s.push(new brach(originX, originY, length, angle));
int iterations = 0;
while (!s.isEmpty()) {
iterations++;
brach b = (brach) s.pop();
System.out.println("branch1 -> x:" + b.x + " y:" + b.y + " lenth:" + b.length + " angle:" + b.angle);
Point endPoints = calculatePoint(b.x, b.y, b.length, b.angle); //Get End point of branch for line to draw
Line line = new Line(b.x, b.y, endPoints.x, endPoints.y); //line object representing the branch
line.setStroke(Color.BLACK); //color set to branch
line.setStrokeWidth(1); //branch color width
drawingPane.getChildren().add(line); // add branch
if (order >= 0) {
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle + 35)); // to draw left branch
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle - 35)); // to draw right branch
order--;
}
}
System.out.println("Iterations = " + iterations);
}
我在堆栈方面遇到问题。我应该如何控制接下来应该去哪个分支对象,以便绘图可以以对称方式进行。有人可以帮忙吗??
这是剩余的代码
paintTree(order, ((int) drawingPane.getWidth() / 2), ((int) drawingPane.getHeight()), frameHeight / 2, 180); //Start drawing from main branch
<小时/>
public Point calculatePoint(int x, int y, double size, double degree) {
Point point = new Point(x, y);
double radians = Math.PI / 180 * degree; // rad = 180°/π ,1° = π/180° so radians = degrees × π / 180°
point.x += (int) (size * Math.sin(radians)); //new x point for end of branch
point.y += (int) (size * Math.cos(radians)); //new y point for end of branch
return point;
}
<小时/>
class brach {
int x;
int y;
double length;
double angle;
public brach(int x, int y, double length, double angle) {
this.x = x;
this.y = y;
this.length = length;
this.angle = angle;
}
}
最佳答案
向左或向右时需要“切换”。
像这样:
boolean goLeft = false;
while (!s.isEmpty()) {
...
if (order >= 0) {
if(goLeft) {
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle + 35)); // to draw left branch
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle - 35)); // to draw right branch
goLeft = false;
} else {
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle - 35)); // to draw right branch
s.push(new brach(endPoints.x, endPoints.y, b.length / 2, b.angle + 35)); // to draw left branch
goLeft = true;
}
order--;
}
}
关于java - 在java fx中使用堆栈和循环绘制树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43529575/