title: Drawing tree using stack and loop in java fx
body: <p>i want to draw </p>
  
  <p><a href="https://i.stack.imgur.com/EQZvu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EQZvu.jpg" alt="enter image description here"></a></p>
  
  <p>i did it perfectly using recursion </p>
  
  <pre><code>private void paintTree2(int order, int originX, int originY, int length, double angle) {
      if (order == -1) {
          return;
      }
      System.out.println("branch2 -&gt; 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
  }
  </code></pre>
  
  <p>but when i try to go with stack and loop.i get </p>
  
  <p><a href="https://i.stack.imgur.com/V2vTr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V2vTr.jpg" alt="enter image description here"></a></p>
  
  <pre><code>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 -&gt; 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 &gt;= 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);
  }
  </code></pre>
  
  <p>I'm having problem with stack.how should i control which branch object should go next so drawing can go in symmetrical way. can someone help??</p>
  
  <p>here's remaining code</p>
  
  <pre><code>paintTree(order, ((int) drawingPane.getWidth() / 2), ((int) drawingPane.getHeight()), frameHeight / 2, 180); //Start drawing from main branch
  </code></pre>
  
  <hr>
  
  <pre><code>    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;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>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;
  }
  }
  </code></pre>