fractalkitty looking for math? fractalkitty.com

Genuary 2022

Orbs of Noise

#noise#random#trig#vectors#classes#interactive#fractal#polar#packing#genuary

nf2nArraycArraycBooladdCirclenoiseCircletheta

let n = 90;
let nf,nf2;
let d;
let nArray = [];
let cArray = [];
let t = 0;
let cnt;
let cBool = true;
let r1, g1, b1;
function setup() {
  n = int(random(10, 100));
  nf = random(0.0000001, 0.05);//change this variable and the next to change the flow
  nf2 = random(2, 13);
  cnt = 0;
  m = windowWidth;
  cnv = createCanvas(m, m);
  cArray[0] = [];
  cArray[1] = [];
  cArray[2] = [];
  d = min(width, height) * 0.9;
  i = 0;
  while (cnt <= n) {
    addCircle(i);
    i++;
  }
  r1 = random(0, 120);
  g1 = random(30, 150);
  b1 = random(30, 200);
}
function draw() {
  strokeWeight(0.09);
  translate(width / 2, height / 2);
  for (let i = 0; i <= n; i++) {
    for (let j = 0; j < 2; j++) {
      nArray[i].move();
    }
  }
  if (frameCount > 15) {
    noLoop();
  }
  if (frameCount === 1) {
    background(305 - r1, 305 - g1, 305 - b1);
  }
}

function addCircle(s) {
  x = random(-d / 2, d / 2);
  y = random(-d / 2, d / 2);
  c = 0;
  a = dist(x, y, 0, 0);
  b = 10000000;
  if (a <= d / 2) {
    b = (d / 2 - a) * 2;
    if (cnt === 0) {
      cBool = true;
    } else {
      for (let i = 0; i < cnt; i++) {
        cR = cArray[2][i] / 2;
        k = dist(cArray[0][i], cArray[1][i], x, y);
        c = 2 * (k - cArray[2][i] / 2);
        b = min(b, c);
        if (k - cR < 0) {
          cBool = false;
        }
      }
    }
  } else if (a > d / 2) {
    cBool = false;
  }
  if (cBool) {
    cArray[0][cnt] = x;
    cArray[1][cnt] = y;
    cArray[2][cnt] = b;
    nArray.push(new noiseCircle(b / 2, x, y));
    cnt++;
  }
  cBool = true;
}
class noiseCircle {
  constructor(r, x, y) {
    this.x = x;
    this.y = y;
    this.p = [];
    this.r = r;
    this.n = 0;
    this.t = 0;
    this.np = int(r * 100);
    for (i = 0; i < this.np; i++) {
      let c = random(this.r);
      let theta = random(-PI, PI);
      this.p[i] = createVector(c * sin(theta), c * cos(theta));
    }
  }
  move() {
    push();
    translate(this.x, this.y);
    stroke(r1, g1, b1);
    this.t++;
    for (let p = 0; p < this.np; p++) {
      let n = noise(this.p[p].x * nf, this.p[p].y * nf) * TWO_PI;
      this.p[p].add(cos(n * nf2), sin(n * nf2));
      let theta = asin(
        this.p[p].y /
          sqrt(this.p[p].x * this.p[p].x + this.p[p].y * this.p[p].y)
      );
      let px = constrain(
        this.p[p].x,
        this.r * sin(theta - PI / 2),
        this.r * cos(theta)
      );
      let py = constrain(
        this.p[p].y,
        -this.r * sin(theta),
        this.r * cos(theta + PI / 2)
      );
      strokeWeight(0.5);
      point(px, py);
    }
    pop();
  }
}

function keyPressed() {
  if (keyCode === 32) {
    cArray = [];
    nArray = [];
    t = 0;
    cnt = 0;
    cBool = true;
    frameCount = 0;
    setup();
    loop();
    background(255);
    draw();
  } else if (keyCode === 78) {
    save(cnv, n + "packing" + round(nf, 4) + "_" + round(nf2, 4) + ".png");
    z++;
  }
}