algorithmrecursiondrawingprocessingfractals

Drawing fractal trees only using line function


I'm trying to figure out how to draw fractal trees without using any geometrical transformations.

I have come up with this code, but it does not rotate properly for further branches.

void setup() {
  size(1000,1000);
  background(50);
  stroke(255);
}

void draw() {
  branch(100, width/2, height, 10, PI/2);
}

float angle = PI/6;
void branch(float size, float cx, float cy, int noi, float alpha) {

  if(noi != 0) { //Number of increments - noi
    float rx = cx + (cos(alpha) * size);
    float lx = cx - (cos(alpha) * size);
    float y = cy - (sin(alpha) * size);

    line(cx, cy, rx, y);
    line(cx, cy, lx, y);

    branch(size/2, rx, y, noi-1, alpha - angle);
    branch(size/2, lx, y, noi-1, alpha - angle);

  } else {
    return;

  }

}

I used basic trigonometric conversions for finding the next right and left point. I think i'm not using the correct alpha values for conversions.

Right now i get this

Trying to draw this


Solution

  • I solved the problem.

    void setup() {
      size(1000,1000);
      background(50);
      stroke(255);
    }
    
    void draw() {
      branch(100, width/2, height/2, 10, PI/2);
    }
    
    float angle = PI/6;
    void branch(float size, float cx, float cy, int noi, float alpha) {
    
      if(noi != 0) { //Number of increments - noi
        float rx = cx + (cos(alpha) * size);
        //float lx = cx - (cos(alpha) * size);
        float y = cy - (sin(alpha) * size);
    
        line(cx, cy, rx, y);
        //line(cx, cy, rx, y);
    
        branch(size*0.66, rx, y, noi-1, alpha - angle);
        branch(size*0.66, rx, y, noi-1, alpha + angle);
    
      } else {
        return;
    
      }
    
    }