function gaussian(x) {
return 180 * Math.pow(Math.E, -(x*x/3000));
}
let samples = [];
function loop(ctx, frame) {
// drawFunction automatically shifts x and y,
// but returns a function that maps
// shift(x,y) -> [x',y'] to the new coordinate system
let shift = drawFunction(ctx, gaussian, -200, frame * 4 - 200, { y_offset: 10 });
let attempts = 100;
while (attempts--) {
let x = Math.random() * 1000 - 500;
let y = Math.random() * 200;
if (y < gaussian(x)) {
samples.push(shift(x,y));
break;
}
}
// globals don't play nice
if (frame === 0) { samples = []; }
for (let p of samples) {
drawPoint(ctx, p[0], p[1]);
}
}