java - 在java fx中使用堆栈和循环绘制树

标签 java loops recursion javafx stack

我想画

enter image description here

我使用递归完美地完成了

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
}

但是当我尝试使用堆栈和循环时,我得到

enter image description here

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/

相关文章:

java - spring-aop 是否可以抛出检查异常?

java - Java中的异步IO?

java - ThreadLocal 的目的?

ruby - 将散列值搜索到另一个散列中并创建具有匹配值的新散列

javascript - 尝试在 JavaScript 中递归查找属性

c - 在单个递归函数中查找数组的最大值和最小值

java - 从 StringBuilder 获取子字符串,并通过 lastIndexOf 计算结束索引,抛出 ArrayIndexOutOfBoundsException

algorithm - 依赖for循环的时间复杂度

javascript - jQuery Loop by id 为每个元素

javascript - 如何提取嵌套对象数组中 'enabled' 对象的路径