使用堆和循环的图画树在Java fx

时间:2017-04-20 20:44:14

标签: 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
}

但是当我尝试使用stack和loop.i时

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;
}
}

1 个答案:

答案 0 :(得分:1)

左转或右转时需要“切换”。

像这样:

fun incList ([] : int list) = []
        | incList ([x] : int list) = [x]
        | incList (h1::h2::t1 : int list) = if h2>h1 then h1::(incList 
(h2::t1)) else (incList (h1::t1))