by sgentle · 8y ago · 0 comments
Here's a version I extended to three dimensions, designed to be run in the JS console of the original article (it'll show up underneath the first sphere):
new SphereSimulation(document.getElementById("spheres1"), () => {
const rand = Math.random
const root = (x) => rand() < 0.5 ? Math.sqrt(x) : -Math.sqrt(x)
const invdist = (a=0, b=0) => root(RADIUS*RADIUS - a*a - b*b)
const x1 = rand() * invdist()
const x2 = rand() * invdist(x1)
const y2 = rand() * invdist(x2)
const x3 = rand() * invdist(x2, y2)
const y3 = rand() * invdist(x3, y2)
const z3 = rand() * invdist(x3, y3)
return new THREE.Vector3(x3, y3, z3)
})
It looks okay to me - am I missing something? Is this a reasonable approach?Your approach samples uniformly from the radius, so you're going to end up with far too many samples close to the center.