fractalkitty looking for math? fractalkitty.com

all

MC_points

#random#interactive

pt1pt2minMax

let pt1, pt2;
let n, s;
function setup() {
  createCanvas(500, 500);
  background(200);
  rect(0, 0, width, height);
  noStroke();
  n = 0;
  s = 0;
}

function draw() {
  pt1 = { x: random(0, width), y: random(0, height), w: -99, d: width };
  pt2 = { x: random(0, width), y: random(0, height), w: -99, d: width };
  for (let i = 0; i < 1; i++) {
    minMax();
  }
}

function minMax() {
  let m = min(pt1.x, abs(width - pt1.x), pt1.y, abs(height - pt1.y));
  if (pt1.x === m) {
    pt1.w = 0;
  } else if (abs(width - pt1.x) === m) {
    pt1.w = 1;
  } else if (pt1.y === m) {
    pt1.w = 2;
  } else if (abs(height - pt1.y) === m) {
    pt1.w = 3;
  }
  pt1.d = m;

  let m2 = min(pt2.x, abs(width - pt2.x), pt2.y, abs(height - pt2.y));
  if (pt2.x === m2) {
    pt2.w = 0;
  } else if (abs(width - pt2.x) === m2) {
    pt2.w = 1;
  } else if (pt2.y === m2) {
    pt2.w = 2;
  } else if (abs(height - pt2.y) === m2) {
    pt2.w = 3;
  }
  pt2.d = m2;

  if (m <= m2) {
    q = pt1;
    p = pt2;
  } else {
    q = pt2;
    p = pt1;
  }
  if (q.w === 0) {
    h = 0;
    k =
      ((q.x - h) * (q.x - h) - (p.x - h) * (p.x - h) - p.y * p.y + q.y * q.y) /
      (-2 * p.y + 2 * q.y);
  } else if (q.w === 1) {
    h = width;
    k =
      ((q.x - h) * (q.x - h) - (p.x - h) * (p.x - h) - p.y * p.y + q.y * q.y) /
      (-2 * p.y + 2 * q.y);
  } else if (q.w === 2) {
    k = 0;
    h =
      ((q.y - k) * (q.y - k) - (p.y - k) * (p.y - k) - p.x * p.x + q.x * q.x) /
      (-2 * p.x + 2 * q.x);
  } else if (q.w === 3) {
    k = height;
    h =
      ((q.y - k) * (q.y - k) - (p.y - k) * (p.y - k) - p.x * p.x + q.x * q.x) /
      (-2 * p.x + 2 * q.x);
  }
  if (h >= 0 && h <= width && k >= 0 && k <= height) {
    noFill();
    stroke(0, 0, 0, 50);
    strokeWeight(0.5);
    line(h, k, p.x, p.y);
    line(h, k, q.x, q.y);
    n++;
    s++;

    fill(0, 0, 100, 150);
    circle(q.x, q.y, 2);
    fill(100, 0, 0, 150);
    circle(p.x, p.y, 2);
  } else {
    fill(150, 150, 150, 100);
    circle(p.x, p.y, 1);
    n++;
  }
}
function mousePressed() {
  console.log("p = " + str(round(s / n, 10)));
  console.log("n = " + str(n));
  draw();
}

function keyPressed() {
  noLoop();
}