xxxxxxxxxx
48
let r = 100;
let orig;
let tol = 0.0001;
let alp;
let p;
let pold;
let go = 1;
let n;
let C;
function setup() {
alp = (3 - sqrt(5)) * PI;
createCanvas(400, 400);
pold = -1;
p = createVector(0, r);
orig = p.copy();
background(220);
n = 0;
C = 2*sin(alp/2);
}
function draw() {
if (go) {
n++;
push();
translate(width / 2, height / 2);
stroke(0);
strokeWeight(0.5);
noFill();
ellipse(0, 0, 2 * r, 2 * r);
p = rot(p, alp * 2.0);
if (pold != -1)
line(pold.x, pold.y, p.x, p.y);
pop();
pold = p.copy();
if (p.copy().sub(orig).mag()<(tol*r)){
go = 0;
text((n).toFixed(0) + " bounces",20,20);
text((n*C).toFixed(2) + " metres covered",20,40);
}
}
}
function rot(v, theta) {
let vx = v.x * cos(theta) - v.y * sin(theta);
let vy = v.x * sin(theta) + v.y * cos(theta);
return createVector(vx, vy);
}