fractalkitty looking for math? fractalkitty.com

weekly creative coding

hold still! - bacteriophage

#random#trig#vectors#interactive#wcc

playBooltailFiberssheathlayeringdarkerhead

let v;
let sx, sy, sh;
let playBool = true;
function setup() {
  cnv = createCanvas(400, 400);
  background(205);
  v = createVector(random(width / 3, width / 2), random(width / 3, width / 2));
  frameRate(1);
}
function draw() {
  if (playBool) {
    background(205, 205, 205, 50);
    layering(40);
    darker(40, v, 40000, random(30, 50));
    head(v, random(30, 50), random(5, 6));
    for (let i = 0; i < random(5, 20); i++) {
      stroke(255, 255, 255, 100);
      strokeWeight(random(4));
      push();
      rotate(random(PI));
      arc(
        random(width),
        random(height),
        random(100),
        random(50),
        random(0),
        random(PI)
      );
      pop();
      fill(255, 255, 255, 20);
      noStroke();
      vt = createVector(random(width), random(height));
      circle(vt.x, vt.y, random(5, 100));
      for (let j = 0; j < 20; j++) {
        circle(vt.x + random(10), vt.y + random(10), random(5, 10));
      }
    }

    tailFibers(sx, sy, 10, 0.5);

    sheath();
    tailFibers(sx, sy, 50, 2);
    layering(30);
    darker(20, v, 20000, 40);
  } else {
    background(205, 205, 205);
    layering(40);
    darker(40, v, 40000, random(30, 50));
    head(v, random(30, 50), random(5, 6));
    for (let i = 0; i < random(5, 20); i++) {
      stroke(255, 255, 255, 100);
      strokeWeight(random(4));
      push();
      rotate(random(PI));
      arc(
        random(width),
        random(height),
        random(100),
        random(50),
        random(0),
        random(PI)
      );
      pop();
      fill(255, 255, 255, 20);
      noStroke();
      vt = createVector(random(width), random(height));
      circle(vt.x, vt.y, random(5, 100));
      for (let j = 0; j < 20; j++) {
        circle(vt.x + random(10), vt.y + random(10), random(5, 10));
      }
    }

    tailFibers(sx, sy, 10, 0.5);

    sheath();
    tailFibers(sx, sy, 50, 2);
    layering(30);
    darker(20, v, 20000, 40);
    noLoop();
  }
  stroke(0);
  strokeWeight(1);
  line(20, height - 20, 70, height - 20);
  line(20, height - 15, 20, height - 25);
  line(70, height - 15, 70, height - 25);
  noStroke();
  fill(0);
  text("50nm", 30, height - 25);
}
function tailFibers(x, y, l, w) {
  strokeWeight(w);
  stroke(225);
  for (let i = 0; i < 6; i++) {
    tmp = createVector(random(x - l, y + l), random(sy - l, sy + l));
    line(x, y, tmp.x, tmp.y);
    line(tmp.x, tmp.y, tmp.x + random(l / 2), tmp.y + random(l / 2));
  }
  strokeWeight(1);
}
function sheath() {
  s = random(2, 12);
  stroke(20, 20, 20, 50);
  strokeWeight(1);
  sh = [];
  sh[0] = createVector(sx, sy);
  circle(sx, sy, s);
  ran1 = random(30, 60);
  for (let i = 1; i < ran1; i++) {
    sh[i] = createVector(
      sh[i - 1].x + i / 20,
      sh[i - 1].y + (s * sin((PI / ran1) * i)) / 5
    );
    if (i % 10 === 0) {
      darker(10, sh[i], 5000, 10);
    }
  }
  fill(255, 255, 255, 230);
  stroke(20, 20, 20, 50);
  strokeWeight(1);
  for (let i = 0; i < ran1; i++) {
    if (i == 10) {
      circle(sh[i].x, sh[i].y, s * 2);
    } else {
      circle(sh[i].x, sh[i].y, s);
    }
  }
  sx = sh[sh.length - 1].x;
  sy = sh[sh.length - 1].y;
}
function layering(p) {
  stroke(40, 40, 40, p);
  for (let i = 0; i < 80000; i++) {
    point(random(width), random(width));
  }
}
function darker(p, vect, nd, sd) {
  stroke(0, 0, 0, p);
  strokeWeight(1);
  for (let i = 0; i < nd; i++) {
    point(randomGaussian(vect.x, sd), randomGaussian(vect.y, sd));
  }
}
function head(v, r, n) {
  stmp = [];
  fill(250, 250, 250, 250);
  noStroke();
  angle = TWO_PI / n;
  beginShape();
  strokeWeight(0.5);
  cnt = 0;
  for (let a = -4 * angle; a < TWO_PI - 4 * angle; a += angle) {
    sx = v.x + cos(a) * r + random(-r / 3, r / 3);
    sy = v.y + sin(a) * r + random(-r / 3, r / 3);
    stmp[cnt] = createVector(sx, sy);
    vertex(sx, sy);
    cnt++;
  }
  endShape(CLOSE);
  stroke(180);
  for (let i = 0; i < stmp.length; i++) {
    strokeWeight(0.5);
    line(v.x, v.y, stmp[i].x, stmp[i].y);
  }
  stroke(40, 40, 40, 40);
  noFill();
}

function mousePressed() {
  playBool = !playBool;
  if (playBool) {
    loop();
  }
}