fractalkitty looking for math? fractalkitty.com

weekly creative coding

Petri Architected

#random#trig#classes#interactive#animation#wcc

cArraysArraybeingsstrands

let n = 50;
let n2 = 20;
let a, b;
let cArray = [];
let sArray = [];
let d;
function setup() {
  d = min(windowWidth, windowHeight) * 0.8;
  createCanvas(d, d);
  c3 = color(random(0, 55), random(0, 155), random(0, 255));
  c4 = color(random(0, 55), random(0, 155), random(0, 255), random(10, 200));
  a = int(random(3, 4));
  b = int(random(5, 6));
  for (let i = 0; i < n; i++) {
    cArray.push(new beings());
  }
  for (let i = 0; i < n2; i++) {
    sArray.push(new strands());
  }
}

function draw() {
  background(c3);
  t = TWO_PI * sin(frameCount / 2000);
  translate(width / 2, height / 2);
  noStroke();
  fill(c4);
  circle(0, 0, width);
  rotate(-t / 3);
  for (i = 0; i < n; i++) {
    cArray[i].move();
  }
  for (let i = 0; i < n2; i++) {
    push();
    rotate(t);
    sArray[i].move();
    for (let j = 1; j < 40; j++) {
      strokeWeight(2);
      stroke(i / 4 + 50, i / 5 + 50, i / 6 + 50, 30);
      circle(
        (d / 2) * sin((i * b * PI) / a + t),
        (d / 2) * cos((i * a * PI) / b + t),
        a / 3
      );
      stroke(255 - i * 10, 255 - i * 8, 255 - i * 5, 50);
      point(
        (d / 7) * sin(i * j + b * t),
        (d / 7) * cos(i * j + b * t) * cos((t / a) * i)
      );
    }
    pop();
  }
}

function mousePressed() {
  c3 = color(random(0, 55), random(0, 155), random(0, 255));
  a = int(random(1, 20));
  b = int(random(1, 20));
  c4 = color(random(0, 55), random(0, 155), random(0, 255), random(10, 200));
  d = min(windowWidth, windowHeight) * 0.8;
  resizeCanvas(d, d);
}

class beings {
  constructor() {
    this.x = random(-width / 2, width / 2);
    this.y = random(-height / 2, height / 2);
    this.r1 = random(10, width / 3);
    this.r2 = this.r1 - random(10, width / 4);
    this.color1 = color(
      random(0, 180),
      random(0, 210),
      random(0, 210),
      random(0, 50)
    );
    this.color2 = color(
      random(100, 255),
      random(150, 255),
      random(150, 255),
      random(0, 50)
    );
    this.t = 0;
    this.dt = random(0.01, 2);
  }
  move() {
    this.t = t * this.dt;
    fill(this.color1);
    circle(
      this.x + (this.r1 / 4) * sin(this.t - this.y) * cos(this.t / this.x),
      this.y + (this.r1 / 4) * cos(this.t),
      this.r1
    );
    fill(this.color2);
    circle(
      this.x + (this.r1 / 4) * sin(this.t - this.y) * cos(t / this.x),
      this.y + (this.r1 / 4) * cos(this.t),
      this.r2
    );
  }
}

class strands {
  constructor() {
    this.n = int(random(50, 200));
    this.x = [];
    this.y = [];
    this.x[0] = random(-width / 2, width / 2);
    this.y[0] = random(-height / 2, height / 2);
    for (let i = 1; i < this.n; i++) {
      this.x[i] = this.x[i - 1] + random(-2, 3);
      this.y[i] = this.y[i - 1] + random(-3, 2);
    }
  }
  move() {
    for (let i = 1; i < this.n; i++) {
      push();
      rotate(i / 100);
      stroke(i / 14, i + 100, i + 150, 150);
      let xt = this.x[i] - (this.n / 40) * sin((i * t) / 20);
      let yt = this.y[i] - (this.n / 40) * cos((i * t) / 20);
      point(xt, yt);
      point(yt, xt);
      circle(
        (d / 2) * sin((i * b * PI) / a + t),
        (d / 2) * cos((i * a * PI) / b + t),
        a / 3
      );
      pop();
    }
  }
}