fractalkitty looking for math? fractalkitty.com

100 days of code

bFerny

#random#interactive#math#fractal#plants

nPointspArray

let x = 0;
let y = 0;
let nPoints = 20000;
let px =[];
let py=[];
let n=9;
function setup() {
  createCanvas(600, 600);
  background(100,100,50);
  angleMode(DEGREES);
  pArray(); 
}
function draw() {
  background(100,100,50,150);
  t=frameCount/2
  translate(width/2,height/2);
  stroke(50,50,10);
  strokeWeight(0.5);
  push()
    translate(100,0)
    scale(-1.5,2.2)
    for (let i = 0; i < nPoints; i++) {
      point(px[i]*40,(py[i]-9.9983/2)*22)
    }
    scale(-1,-1)
    translate(-135,0)
    for (let i = 0; i < nPoints; i++) {
      point(px[i]*40,(py[i]-9.9983/2)*22)
    }
  pop()
  stroke(170,180,60)
  strokeWeight(1);
  for(let j=0;j<n;j++){
    push()
      rotate(j*(360/n)+t)
      for (let i = 0; i < nPoints; i++) {
        point(px[i]*20,(py[i]-9.9983/2)*22+100)
      }
    pop()
  }
  noLoop()
}

function pArray(){
  px[0]=0;
  py[0]=0;
  for (let i=1;i<nPoints;i++){
    let r = random(1);
    if (r < 0.001) {//stems
      px[i] = 0;
      py[i] = 0.16 * py[i-1];
    } else if (r < 0.86) {
      px[i] = 0.85 * px[i-1] + 0.04 * py[i-1];
      py[i] = -0.04 * px[i-1] + 0.85 * py[i-1] + 1.60;
    } else if (r < 0.93) {
      px[i] = 0.20 * px[i-1] + -0.36 * py[i-1];
      py[i] = 0.23 * px[i-1] + 0.22 * py[i-1] + 1.60;
    } else {
      px[i] = -0.15 * px[i-1] + 0.028 * py[i-1];//changed to fold (was0.28)
      py[i] = 0.26 * px[i-1] + 0.24 * py[i-1] + 0.44;
    }
  }
  
}

function mousePressed(){
  translate(-width/2,-height/2);
  n=n+1;
  if (n>20){
    n=1;
  }
  draw()
}