fractalkitty looking for math? fractalkitty.com

weekly creative coding

parametric pieces

#random#trig#classes#interactive#poem#math#fractal#parametric#wcc

piecestimeOfNowjiggleBoolred1green1blue1PiecesOfYouaLittleExtra


let pieces;
let timeOfNow;
let jiggleBool = false;
let red1, green1, blue1;
function setup() {
  createCanvas(600, 600);
  pieces = [];
  frameCount = 0;
  angleMode(DEGREES);
  strokeWeight(0.5);
  red1 = random(0, 25);
  green1 = random(10, 25);
  blue1 = random(10, 25);
  mixer = random(1, 3);
  background(mixer * red1, mixer * green1, mixer * blue1);
  pieces.push(new PiecesOfYou(width / 4, width / 4, width / 2));
}
function draw() {
  timeOfNow = frameCount / 2;
  translate(width / 2, width / 2);

  for (let i = 0; i < pieces.length; i++) {
    pieces[i].translate();
    pieces[i].beSeen();
  }
  if (timeOfNow % 180 === 0) {
    noLoop();
  }
}

class PiecesOfYou {
  constructor(x, y, w) {
    this.x = -width / 2 + x;
    this.y = -width / 2 + y;
    this.x1 = this.x;
    this.y1 = this.y;
    this.w = w;
    this.mix = random(0.5, 15);
    this.mix2 = random(10, 20);
    this.c = color(
      red1 * this.mix + random(-20, 20),
      green1 * this.mix + random(-20, 20),
      blue1 * this.mix + random(-20, 20)
    );
    this.s = color(red1 * this.mix2, green1 * this.mix2, blue1 * this.mix2);
    this.uniqueness = random(-width / 5, width / 5);
    this.a1 = random(-4, 4);
    this.b1 = random(-4, 4);
    this.c1 = random(-4, 4);
  }
  beSeen() {
    fill(this.c);
    stroke(this.s);
    rect(this.x, this.y, this.w, this.w);
  }
  translate() {
    let aLittleExtra = 0;
    if (jiggleBool) {
      aLittleExtra = random(-5, 5) * sin(timeOfNow);
    } else {
      aLittleExtra = 0;
    }
    this.x =
      this.x1 +
      ((this.a1 * this.w) / 4) * sin(timeOfNow * this.c1) * sin(timeOfNow) +
      this.uniqueness * sin(timeOfNow) * cos(timeOfNow);
    this.y =
      this.y1 +
      ((this.b1 * this.w) / 4) * cos(timeOfNow - this.x) * sin(timeOfNow) +
      this.uniqueness * sin(timeOfNow) * sin(timeOfNow * this.b1) -
      aLittleExtra;
  }
}

function mousePressed() {
  loop();
  thisOne = -1;
  for (let i = 0; i < pieces.length; i++) {
    if (
      mouseX >= width / 2 + pieces[i].x &&
      mouseX <= width / 2 + pieces[i].x + pieces[i].w &&
      mouseY >= width / 2 + pieces[i].y &&
      mouseY <= width / 2 + pieces[i].y + pieces[i].w
    ) {
      thisOne = i;
    }
  }
  if (thisOne != -1) {
    pieces.push(
      new PiecesOfYou(
        pieces[thisOne].x1 + width / 2,
        pieces[thisOne].y1 + width / 2,
        pieces[thisOne].w / 2
      )
    );
    pieces.push(
      new PiecesOfYou(
        pieces[thisOne].x1 + width / 2 + pieces[thisOne].w / 2,
        pieces[thisOne].y1 + width / 2,
        pieces[thisOne].w / 2
      )
    );
    pieces.push(
      new PiecesOfYou(
        pieces[thisOne].x1 + width / 2,
        pieces[thisOne].y1 + width / 2 + pieces[thisOne].w / 2,
        pieces[thisOne].w / 2
      )
    );
    pieces.push(
      new PiecesOfYou(
        pieces[thisOne].x1 + width / 2 + pieces[thisOne].w / 2,
        pieces[thisOne].y1 + width / 2 + pieces[thisOne].w / 2,
        pieces[thisOne].w / 2
      )
    );
    pieces.splice(thisOne, 1);
  }
}

function keyPressed() {
  if (keyCode === 74) {
    jiggleBool = !jiggleBool;
  }
  if (keyCode === 82) {
    loop();
    setup();
  }
}