xxxxxxxxxx
135
// Courtesy of codingJM
// https://editor.p5js.org/codingJM/sketches
let fractals = [];
let count = 12,
nb, rtimer = 70;
//vars for noise
let xoff = 0;
let n;
function setup() {
createCanvas(500, 500);
background(0);
//Nb of axes
count = 4;
//time to create recursity
rtimer = 80;
// Counting the nb of object
// Here for 4 recurive patterns
nb = pow(count,4)+pow(count,3)+pow(count,2)+pow(count,1);
// Here for 3 recurive patterns
// nb = pow(count, 3) + pow(count, 2) + pow(count, 1);
//assign some vars for first fractal objet
let p = createVector(0, 0);
let ra = 1;
let v = 2;
//initialize 1rst fractal with a position, a radius, a velocity and a timer
let f = new Fractal(p, ra, v, rtimer);
// add fractal to array
fractals.push(f);
}
function draw() {
background(0, 20);
push();
translate(width / 2, height / 2);
//initialize noise
n = noise(xoff);
// draw each fractal objects
for (index in fractals) {
// update and display the object
fractals[index].update();
fractals[index].display();
// If each fractal is ready to duplicate itself
if (fractals[index].time2fractalise()) {
// Only 585 fractals
// if (fractals.length < 585) {
// console.log(fractals.length);
if (fractals.length < nb) {
for (let i = 0; i < count; i++) {
// for positioning each new 8 particles around its parent, we need angle and a random radius
let angle = map(i, 0, count, 0, TWO_PI);
// we send the angle to the fractalise fonction
fractals.push(fractals[index].fractalise(angle, 3));
}
}
// count--;
}
}
// speeed of the noise
xoff += 0.005;
pop();
}
class Fractal {
//initialize with a position, a radius, a velocity and a timer
constructor(start, rad, vel, n) {
this.start = start.copy();
// for children position
this.end = createVector(0, 0);
this.rad = rad;
this.vel = vel;
// for timer
this.timeStart = n;
this.timer = n;
this.col = color(242, 58, 101);
this.col2 = color(50);
this.col3 = color(180);
// for growing state
this.growing = true;
// Utility: a random number
this.ra = floor(random(10));
}
update() {
// update the scale of the object
if (this.growing) {
this.rad += this.vel;
}
}
display() {
rectMode(CENTER);
//1rst Objet
push();
translate(this.start.x, this.start.y);
rotate(frameCount * .01);
strokeWeight(1);
this.col2.setAlpha(200);
stroke(this.col2);
noFill();
ellipse(0, 0, this.rad / n);
pop();
//2nd objet
this.col3.setAlpha(55);
fill(this.col3);
rect(this.start.x, this.start.y, this.rad / (n * 4), this.rad / (n * 4));
}
time2fractalise() {
// When it's a time to add childrens
this.timer--;
// console.log(this.timer);
if (this.timer < 0 && this.growing) {
this.growing = false;
// Take care the main object stop to graowing
return true;
} else {
return false;
}
}
fractalise(angle, nrad) {
// Create a vector for placing a position around a circle
let newr = createVector(cos(angle) * this.rad / 1.5, sin(angle) * this.rad / 1.5);
// Calcul the new position around this position
this.end = p5.Vector.add(this.start, newr);
// send the new object in the main array of fractals
return new Fractal(this.end, this.rad / 5, this.vel * 0.3, this.timeStart);
}
}