# My solution for fractal trees

**URL:** <https://discourse.processing.org/t/my-solution-for-fractal-trees/26058>\
**Category:** Gallery\
**Created:** [December 8, 2020, 7:24pm UTC](https://discourse.processing.org/t/my-solution-for-fractal-trees/26058 "2020-12-08T19:24:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 8, 2020, 7:24pm UTC](https://discourse.processing.org/t/my-solution-for-fractal-trees/26058/1 "2020-12-08T19:24:45Z")

</div>

It is far from an efficient solution but here it is! It is quite short (altho I haven’t seen a standard solution for it)

```auto
ArrayList<String> path = new ArrayList<String>();
int it = 0, maxIt = 10,rep=0;
float cx = 300, cy = 550,x,y,x2,y2, l = 50, ld = 0.9,rot=-PI/2,spin = PI/6;

void setup() {
  size(600,600);
  for(int i = 0; i < pow(2,maxIt); i++) {
    String a = loadZeros(int(binary(i)),maxIt);
    path.add(a);
  }
  background(0);
  stroke(255);
  strokeWeight(8);
  line(cx,cy,cx,cy-l);
  
}

void draw() {
  String load = path.get(rep);
  float crot = rot, cl = l, x = cx, y = cy,x2=x,y2=y;
  strokeWeight(8);
  for(int i = 0; i < maxIt; i++) {
    strokeWeight(8/pow(2,i));
    x+=cos(crot)*cl;
    y+=sin(crot)*cl;
    cl*=ld;
    crot+=spin*((int(load.charAt(i))*2-1));
    line(x,y,x2,y2);
    x2 = x;
    y2 = y;
  }
  rep++;
  if(rep == pow(2,maxIt)) {
    noLoop();
  }
}

String loadZeros(int number, int len) {
  if((number+"").length() < len) {
    String prefix = "";
    for(int i = 0; i < len-(number+"").length(); i++) {
      prefix += "0";
    }
    return(prefix + number);
  } else {
    return(number+"");
  }
}

```

it takes one path at a time, until the end. There is a bug for maxIt (erations) \> 10 which I will fix in the near future! Enjoy!

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 8, 2020, 7:50pm UTC](https://discourse.processing.org/t/my-solution-for-fractal-trees/26058/2 "2020-12-08T19:50:13Z")

</div>

here is an example

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c4f45888ca7e5e6adfbb12c7f5d69a7905424ca2.png)
