Vector3D.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Coordinate from '../geom/Coordinate'
  2. export default class Vector3D {
  3. constructor() {
  4. Vector3D.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._x = null
  8. this._y = null
  9. this._z = null
  10. if (arguments.length === 1) {
  11. const v = arguments[0]
  12. this._x = v.x
  13. this._y = v.y
  14. this._z = v.getZ()
  15. } else if (arguments.length === 2) {
  16. const from = arguments[0], to = arguments[1]
  17. this._x = to.x - from.x
  18. this._y = to.y - from.y
  19. this._z = to.getZ() - from.getZ()
  20. } else if (arguments.length === 3) {
  21. const x = arguments[0], y = arguments[1], z = arguments[2]
  22. this._x = x
  23. this._y = y
  24. this._z = z
  25. }
  26. }
  27. static length(v) {
  28. return Math.sqrt(v.x * v.x + v.y * v.y + v.getZ() * v.getZ())
  29. }
  30. static dot() {
  31. if (arguments.length === 2) {
  32. const v1 = arguments[0], v2 = arguments[1]
  33. return v1.x * v2.x + v1.y * v2.y + v1.getZ() * v2.getZ()
  34. } else if (arguments.length === 4) {
  35. const A = arguments[0], B = arguments[1], C = arguments[2], D = arguments[3]
  36. const ABx = B.x - A.x
  37. const ABy = B.y - A.y
  38. const ABz = B.getZ() - A.getZ()
  39. const CDx = D.x - C.x
  40. const CDy = D.y - C.y
  41. const CDz = D.getZ() - C.getZ()
  42. return ABx * CDx + ABy * CDy + ABz * CDz
  43. }
  44. }
  45. static normalize(v) {
  46. const len = Vector3D.length(v)
  47. return new Coordinate(v.x / len, v.y / len, v.getZ() / len)
  48. }
  49. static create() {
  50. if (arguments.length === 1) {
  51. const coord = arguments[0]
  52. return new Vector3D(coord)
  53. } else if (arguments.length === 3) {
  54. const x = arguments[0], y = arguments[1], z = arguments[2]
  55. return new Vector3D(x, y, z)
  56. }
  57. }
  58. dot(v) {
  59. return this._x * v._x + this._y * v._y + this._z * v._z
  60. }
  61. getZ() {
  62. return this._z
  63. }
  64. subtract(v) {
  65. return Vector3D.create(this._x - v._x, this._y - v._y, this._z - v._z)
  66. }
  67. equals(o) {
  68. if (!(o instanceof Vector3D))
  69. return false
  70. const v = o
  71. return this._x === v._x && this._y === v._y && this._z === v._z
  72. }
  73. normalize() {
  74. const length = this.length()
  75. if (length > 0.0) return this.divide(this.length())
  76. return Vector3D.create(0.0, 0.0, 0.0)
  77. }
  78. divide(d) {
  79. return Vector3D.create(this._x / d, this._y / d, this._z / d)
  80. }
  81. getX() {
  82. return this._x
  83. }
  84. toString() {
  85. return '[' + this._x + ', ' + this._y + ', ' + this._z + ']'
  86. }
  87. length() {
  88. return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z)
  89. }
  90. getY() {
  91. return this._y
  92. }
  93. add(v) {
  94. return Vector3D.create(this._x + v._x, this._y + v._y, this._z + v._z)
  95. }
  96. hashCode() {
  97. let result = 17
  98. result = 37 * result + Coordinate.hashCode(this._x)
  99. result = 37 * result + Coordinate.hashCode(this._y)
  100. result = 37 * result + Coordinate.hashCode(this._z)
  101. return result
  102. }
  103. }