@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
36 lines (26 loc) • 944 B
JavaScript
import { PI2 } from "../../math/PI2.js";
/**
* Unit sphere with Radius = 1
* @see based on python code suggested here: https://stackoverflow.com/questions/5408276/sampling-uniformly-distributed-random-points-inside-a-spherical-volume
* @param {function} random
* @param {number[]} result
* @param {number} result_offset
*/
export function randomPointInSphere(random, result, result_offset) {
const phi = PI2 * random();
const cosTheta = random() * 2 - 1;
const u = random();
const theta = Math.acos(cosTheta);
// compute radius
const r = Math.cbrt(u);
const sinTheta = Math.sin(theta);
//compute coordinates
const rst = r * sinTheta;
const x = rst * Math.cos(phi);
const y = rst * Math.sin(phi);
const z = r * cosTheta;
//write the result
result[result_offset] = x;
result[result_offset + 1] = y;
result[result_offset + 2] = z;
}