Guest User

Untitled

a guest
May 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. let mathjs = require("mathjs");
  2.  
  3. function getRandomPointOnSphere() {
  4. let o = 2 * Math.PI * Math.random();
  5. let a = Math.acos(2 * Math.random() - 1);
  6. return { o, a };
  7. }
  8.  
  9. function polarToCarth(o) {
  10. let x = Math.sin(o.o) * Math.cos(o.a);
  11. let y = Math.sin(o.o) * Math.sin(o.a);
  12. let z = Math.cos(o.o);
  13. return [x, y, z];
  14. }
  15.  
  16. function run() {
  17. let [x1, y1, z1] = polarToCarth(getRandomPointOnSphere());
  18. let [x2, y2, z2] = polarToCarth(getRandomPointOnSphere());
  19. let [x3, y3, z3] = polarToCarth(getRandomPointOnSphere());
  20. let [x4, y4, z4] = polarToCarth(getRandomPointOnSphere());
  21.  
  22. let x = 0;
  23. let y = 0;
  24. let z = 0;
  25.  
  26. let d0 = mathjs.det([
  27. [x1, y1, z1, 1],
  28. [x2, y2, z2, 1],
  29. [x3, y3, z3, 1],
  30. [x4, y4, z4, 1]
  31. ]);
  32.  
  33. let d1 = mathjs.det([
  34. [x, y, z, 1],
  35. [x2, y2, z2, 1],
  36. [x3, y3, z3, 1],
  37. [x4, y4, z4, 1]
  38. ]);
  39.  
  40. let d2 = mathjs.det([
  41. [x1, y1, z1, 1],
  42. [x, y, z, 1],
  43. [x3, y3, z3, 1],
  44. [x4, y4, z4, 1]
  45. ]);
  46.  
  47. let d3 = mathjs.det([
  48. [x1, y1, z1, 1],
  49. [x2, y2, z2, 1],
  50. [x, y, z, 1],
  51. [x4, y4, z4, 1]
  52. ]);
  53.  
  54. let d4 = mathjs.det([
  55. [x1, y1, z1, 1],
  56. [x2, y2, z2, 1],
  57. [x3, y3, z3, 1],
  58. [x, y, z, 1]
  59. ]);
  60.  
  61. if (
  62. (d0 >= 0 && d1 >= 0 && d2 >= 0 && d3 >= 0 && d4 >= 0) ||
  63. (d0 < 0 && d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0)
  64. ) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70.  
  71. let yes = 0;
  72. let no = 0;
  73. for (var i = 0; i < 1000000; i++) {
  74. if (i % 1000 == 0) {
  75. console.log(i);
  76. }
  77. if (run()) {
  78. yes++;
  79. } else {
  80. no++;
  81. }
  82. }
  83. console.log(yes, no, yes / 1000000);
Add Comment
Please, Sign In to add comment