vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

  1. /**
  2. * @prototype Point3d
  3. * @param {Number} [x]
  4. * @param {Number} [y]
  5. * @param {Number} [z]
  6. */
  7. function Point3d(x, y, z) {
  8. this.x = x !== undefined ? x : 0;
  9. this.y = y !== undefined ? y : 0;
  10. this.z = z !== undefined ? z : 0;
  11. }
  12. /**
  13. * Subtract the two provided points, returns a-b
  14. * @param {Point3d} a
  15. * @param {Point3d} b
  16. * @return {Point3d} a-b
  17. */
  18. Point3d.subtract = function(a, b) {
  19. var sub = new Point3d();
  20. sub.x = a.x - b.x;
  21. sub.y = a.y - b.y;
  22. sub.z = a.z - b.z;
  23. return sub;
  24. };
  25. /**
  26. * Add the two provided points, returns a+b
  27. * @param {Point3d} a
  28. * @param {Point3d} b
  29. * @return {Point3d} a+b
  30. */
  31. Point3d.add = function(a, b) {
  32. var sum = new Point3d();
  33. sum.x = a.x + b.x;
  34. sum.y = a.y + b.y;
  35. sum.z = a.z + b.z;
  36. return sum;
  37. };
  38. /**
  39. * Calculate the average of two 3d points
  40. * @param {Point3d} a
  41. * @param {Point3d} b
  42. * @return {Point3d} The average, (a+b)/2
  43. */
  44. Point3d.avg = function(a, b) {
  45. return new Point3d(
  46. (a.x + b.x) / 2,
  47. (a.y + b.y) / 2,
  48. (a.z + b.z) / 2
  49. );
  50. };
  51. /**
  52. * Calculate the cross product of the two provided points, returns axb
  53. * Documentation: http://en.wikipedia.org/wiki/Cross_product
  54. * @param {Point3d} a
  55. * @param {Point3d} b
  56. * @return {Point3d} cross product axb
  57. */
  58. Point3d.crossProduct = function(a, b) {
  59. var crossproduct = new Point3d();
  60. crossproduct.x = a.y * b.z - a.z * b.y;
  61. crossproduct.y = a.z * b.x - a.x * b.z;
  62. crossproduct.z = a.x * b.y - a.y * b.x;
  63. return crossproduct;
  64. };
  65. /**
  66. * Rtrieve the length of the vector (or the distance from this point to the origin
  67. * @return {Number} length
  68. */
  69. Point3d.prototype.length = function() {
  70. return Math.sqrt(
  71. this.x * this.x +
  72. this.y * this.y +
  73. this.z * this.z
  74. );
  75. };
  76. module.exports = Point3d;