title: How to Draw Fractal Iteratively?
body: <p>I currently have a working code that draws a fractal tree using recursion. However, when I try to draw it iteratively, it is not working.  </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import javax.swing.JFrame;
  
   /*
  
   -Used built in Math trig methods to accomodate angling...looked this up online
  
  
   https://stackoverflow.com/questions/30032635/java-swing-draw-a-line-at-a-specific-angle
  
  
  */
  
  
  
  public class Test extends JFrame {
  
      public Test() {
  
          setBounds(100, 100, 800, 600);  //sets the boundary for drawing
  
      }
  
      public void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
  
          System.out.println("x");
  
          if (depth == 6){
              return; //base case here to prevent infinite recursion..
          }
          else {
  
  
              System.out.println("y1");
  
              //embedded portion '(Math.toRadians(angle) * depth * 10.0)'represents angle...
  
              int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);  //vertical shift calculated using the Sine...PARSED to int because method drawLine() accepts only ints as params
              int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);  //hor. shift calculated using the Cos..PARSED to int
  
  
            //  System.out.println("x2: " + x2);//will reflect the change in vertical shift
              //System.out.println("y2: " + y2);//will reflect change in hor. shift
  
              g.drawLine(x1, y1, x2, y2);//value x1 equals previous line...in other word, start at where previously left off
              // notice that the end point (x2 and y2) becomes starting point for each successive call
  
  
              drawTree(g, x2, y2, angle - 20, depth - 1); //DRAWS LEFT SIDE?
  
             // drawTree(g, x2, y2, angle + 20, depth - 1); //DRAWS RIGHT SIDE?
  
          }
  
  
  
          if (depth == 6){
              return; //base case here to prevent infinite recursion..
          }
          else {
  
  
              System.out.println("y2");
  
  
              int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);  //vertical shift calculated using the Sine...PARSED to int because method drawLine() accepts only ints as params
              int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);  //hor. shift calculated using the Cos..PARSED to int
  
  
             // System.out.println("x2: " + x2);//will reflect the change in vertical shift
              //System.out.println("y2: " + y2);//will reflect change in hor. shift
  
              g.drawLine(x1, y1, x2, y2);//value x1 equals previous line...in other word, start at where previously left off
              // notice that the end point (x2 and y2) becomes starting point for each successive call
  
  
             // drawTree(g, x2, y2, angle - 20, depth - 1); //DRAWS LEFT SIDE?
  
              drawTree(g, x2, y2, angle + 20, depth - 1); //DRAWS RIGHT SIDE?
  
          }
  
      }
  
  
  
  
      public void drawIteratively(Graphics g, int x1A, int y1A, int x1B, int y1B, double angleA, double angleB, int depthA, int depthB){
  
          while (depthA != 4) {
  
  
              int x2A = x1A + (int) (Math.cos(Math.toRadians(angleA)) * depthA * 10.0);
              int y2A = y1A + (int) (Math.sin(Math.toRadians(angleA)) * depthA * 10.0);
  
              g.drawLine(x1A, y1A, x2A, y2A);   //remember it must continue drawing from where it left off
  
              angleA = angleA - 20;
              depthA = depthA - 1;
  
              x1A = x2A;
              y1A = y2A;
  
          }
  
          while (depthA != 4) {
  
  
              int x2A = x1A + (int) (Math.cos(Math.toRadians(angleA)) * depthA * 10.0);
              int y2A = y1A + (int) (Math.sin(Math.toRadians(angleA)) * depthA * 10.0);
  
              g.drawLine(x1A, y1A, x2A, y2A);   //remember it must continue drawing from where it left off
  
              angleA = angleA - 20;
              depthA = depthA - 1;
  
              x1A = x2A;
              y1A = y2A;
  
          }
  
          /*
  
          while(depthB != 4){
  
  
              int x2B = x1B + (int) (Math.cos(Math.toRadians(angleB)) * depthB * 10.0);
              int y2B = y1B + (int) (Math.sin(Math.toRadians(angleB)) * depthB * 10.0);
  
              g.drawLine(x1B, y1B, x2B, y2B);
  
              angleB = angleB + 20;
              depthB = depthB - 1;
  
              x1B = x2B;
              y1B = y2B;
  
          }
          */
  
      }
  
  
  
  
  
  
      @Override
      public void paint(Graphics g) {
          g.setColor(Color.BLUE);
  
  
         //drawTree(g, 400, 400, -90, 9); //these values corresponding to original line aka trunk during initial method call
  
          drawIteratively(g, 400, 500, 400,500 ,-90 , -90, 9,9);
  
  
  
  
      }
  
      public static void main(String[] args) {
  
          new Test().setVisible(true);
  
      }
  }
  </code></pre>