Skip to content

Instantly share code, notes, and snippets.

@stackola
Created May 20, 2018 11:11
Show Gist options
  • Save stackola/d18921d6665e7f19084e3b1f2a1cf876 to your computer and use it in GitHub Desktop.
Save stackola/d18921d6665e7f19084e3b1f2a1cf876 to your computer and use it in GitHub Desktop.
let mathjs = require("mathjs");
function getRandomPointOnSphere() {
let o = 2 * Math.PI * Math.random();
let a = Math.acos(2 * Math.random() - 1);
return { o, a };
}
function polarToCarth(o) {
let x = Math.sin(o.o) * Math.cos(o.a);
let y = Math.sin(o.o) * Math.sin(o.a);
let z = Math.cos(o.o);
return [x, y, z];
}
function run() {
let [x1, y1, z1] = polarToCarth(getRandomPointOnSphere());
let [x2, y2, z2] = polarToCarth(getRandomPointOnSphere());
let [x3, y3, z3] = polarToCarth(getRandomPointOnSphere());
let [x4, y4, z4] = polarToCarth(getRandomPointOnSphere());
let x = 0;
let y = 0;
let z = 0;
let d0 = mathjs.det([
[x1, y1, z1, 1],
[x2, y2, z2, 1],
[x3, y3, z3, 1],
[x4, y4, z4, 1]
]);
let d1 = mathjs.det([
[x, y, z, 1],
[x2, y2, z2, 1],
[x3, y3, z3, 1],
[x4, y4, z4, 1]
]);
let d2 = mathjs.det([
[x1, y1, z1, 1],
[x, y, z, 1],
[x3, y3, z3, 1],
[x4, y4, z4, 1]
]);
let d3 = mathjs.det([
[x1, y1, z1, 1],
[x2, y2, z2, 1],
[x, y, z, 1],
[x4, y4, z4, 1]
]);
let d4 = mathjs.det([
[x1, y1, z1, 1],
[x2, y2, z2, 1],
[x3, y3, z3, 1],
[x, y, z, 1]
]);
if (
(d0 >= 0 && d1 >= 0 && d2 >= 0 && d3 >= 0 && d4 >= 0) ||
(d0 < 0 && d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0)
) {
return true;
} else {
return false;
}
}
let yes = 0;
let no = 0;
for (var i = 0; i < 1000000; i++) {
if (i % 1000 == 0) {
console.log(i);
}
if (run()) {
yes++;
} else {
no++;
}
}
console.log(yes, no, yes / 1000000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment