fractalkitty looking for math? fractalkitty.com

Coded Poems

mathober day-23: axiom

#random#trig#image#interactive#animation#poem#geometry#mathober

img1img2pointsmakeWavemoveWavechangeIt


let img1, img2;
let points;
let p1, p2, x1, x2, m;
let a, b, c, d;

function preload() {
  img1 = loadImage("/assets/axiom.jpg");
  img2 = loadImage("/assets/star.png");
}

function setup() {
  createCanvas(600, 600);
  strokeWeight(0.25);
  angleMode(DEGREES);
  stroke(255);
  imageMode(CENTER);
  makeWave();
  p1 = {
    x: random(-width / 2.2, -width / 4),
    y: random(-width / 5, -width / 2)
  };
  p2 = {
    x: random(width / 3, width / 2.2),
    y: random(-width / 5, -width / 2.2)
  };
  x1 = -width / 2;
  x2 = width / 2;
  a1 = random(-100, 100);
  b1 = random(-90, 90);
  c1 = random(-90, 90);
  d1 = random(-120, 120);
  a = a1;
  b = b1;
  c = c1;
  d = d1;
}

function draw() {
  background(0);
  translate(width / 2, height / 2);
  t1 = frameCount / 10;
  a = a1 + 30 * cos(t1 / 10);
  b = b1 - 30 * cos(t1 / 13);
  c = c1 + 50 * cos(t1 / 17);
  d = d1 - 50 * cos(t1 / 9);
  if (mouseIsPressed) {
    console.log(mouseX - width / 2, p1.x);
    mx = mouseX - width / 2;
    my = mouseY - width / 2;
    if (abs(mx - p1.x) < 20 && abs(my - p1.y) < 20) {
      p1.x = mouseX - width / 2;
      p1.y = mouseY - width / 2;
    }
    if (abs(mx - p2.x) < 20 && abs(my - p2.y) < 20) {
      p2.x = mouseX - width / 2;
      p2.y = mouseY - width / 2;
    }
  }
  m = (p1.y - p2.y) / (p1.x - p2.x);
  image(img1, 0, 0, 800, 800);
  t1 = frameCount / 5 + 3;
  strokeWeight(2);
  line(x1, m * (x1 - p1.x) + p1.y, x2, m * (x2 - p1.x) + p1.y);
  image(img2, p1.x, p1.y, 100, 100);
  image(img2, p2.x, p2.y, 100, 100);
  strokeWeight(0.1);

  for (let i = 0; i < points.length; i++) {
    moveWave(points[i].x);
    image(img2, points[i].x, points[i].y, points[i].size, points[i].size);

    line(
      x1,
      m * (x1 - points[i].x) + points[i].y,
      x2,
      m * (x2 - points[i].x) + points[i].y
    );
  }
}

function makeWave() {
  points = [];
  tmp = -width / 1.5;
  n = int(random(width / 20, width / 3));
  for (let i = 0; i < n; i++) {
    tmp = tmp + random(0, (width * 3) / n);
    if (i == int(n / 2)) {
      s = 400;
    } else {
      s = random(10, 50);
    }
    points[i] = {
      x: tmp,
      y: changeIt(tmp),
      size: s
    };
  }
}

function moveWave() {
  for (let i = 0; i < n; i++) {
    if (points[i].x < width / 1.8) {
      points[i].x += randomGaussian(0.005, 0.003);
      points[i].y = changeIt(points[i].x);
    } else {
      points[i].x = -width / 1.8;
      points[i].y = changeIt(points[i].x);
    }
  }
}

function changeIt(x) {
  return (
    a * sin(x) * cos(x) +
    b * sin(x / 2 - a) * tan(x / 4 + a) +
    c * sin(x / 5 - c) * cos(x / 2 - b) +
    d * tan(x / 5 - d) * sin(x / 10 - c) * tan(x / 2)
  );
}

function keyPressed() {
  setup();
  draw();
}