circles in the square
Click on the canvas for an update on data and spoiler to the problem.
let speed = 1000;
let d = 300,
n = 0,
total = 0;
function setup() {
createCanvas(600, 600);
rectMode(CENTER);
background(245, 255, 255);
}
function draw() {
noFill();
strokeWeight(0.5);
translate(width / 2, height / 2);
rect(0, 0, d, d);
stroke(255, 255, 255, 2);
for (let i = 0; i < speed; i++) {
x1 = random(-d / 2, d / 2);
x2 = random(-d / 2, d / 2);
y1 = random(-d / 2, d / 2);
y2 = random(-d / 2, d / 2);
r = dist(x1, y1, x2, y2) / 2;
cx = x1 + (x2 - x1) / 2;
cy = y1 + (y2 - y1) / 2;
stroke(random(100, 255), random(150, 255), random(150, 255), 10);
circle(cx, cy, r * 2);
if (
cx + r > d / 2 ||
cx - r < -d / 2 ||
cy + r > d / 2 ||
cy - r < -d / 2
) {
n++;
}
total++;
}
stroke(255, 255, 255, 5);
fill(235, 255, 255, 5);
rect(0, 0, d, d);
push();
fill(235, 255, 255);
if (mouseIsPressed) {
rect(-width / 2 + 20, -height / 2 + 20, 280, 140);
fill(0);
stroke(0);
text("total: " + total, -width / 2 + 20, -height / 2 + 20);
text("n: " + n, -width / 2 + 20, -height / 2 + 40);
p = round(n / total, 10);
text("p: " + p, -width / 2 + 20, -height / 2 + 60);
text("1 - π/6 ~ 0.476401224402", -width / 2 + 20, -height / 2 + 80);
pop();
}
}