Source: math/Point.js

  1. /* eslint max-len: "off" */
  2. /**
  3. * 二维空间内坐标点类
  4. *
  5. * @class
  6. * @memberof JC
  7. * @param {number} [x=0] x轴的位置
  8. * @param {number} [y=0] y轴的位置
  9. * @param {number} [z=0] z轴的位置
  10. * @param {number} [w=0] w轴的位置
  11. */
  12. function Point(x, y, z, w) {
  13. /**
  14. * @member {number}
  15. * @default 0
  16. */
  17. this.x = x || 0;
  18. /**
  19. * @member {number}
  20. * @default 0
  21. */
  22. this.y = y || 0;
  23. /**
  24. * @member {number}
  25. * @default 0
  26. */
  27. this.z = z || 0;
  28. /**
  29. * @member {number}
  30. * @default 0
  31. */
  32. this.w = w || 0;
  33. }
  34. /**
  35. * 克隆一这个坐标点
  36. *
  37. * @return {JC.Point} 克隆的坐标点
  38. */
  39. Point.prototype.clone = function() {
  40. return new Point(this.x, this.y, this.z, this.w);
  41. };
  42. /**
  43. * 拷贝传入的坐标点来设置当前坐标点
  44. *
  45. * @param {JC.Point} p
  46. */
  47. Point.prototype.copy = function(p) {
  48. this.set(p.x, p.y, p.z, p.w);
  49. };
  50. /**
  51. * 设置坐标点
  52. *
  53. * @param {number} x 轴的位置
  54. * @param {number} y 轴的位置
  55. * @param {number} z 轴的位置
  56. * @param {number} w 轴的位置
  57. * @return {Point} this
  58. */
  59. Point.prototype.set = function( x, y, z, w ) {
  60. this.x = x;
  61. this.y = y;
  62. this.z = z;
  63. this.w = w;
  64. return this;
  65. };
  66. Point.prototype.add = function( v, w ) {
  67. if ( w !== undefined ) {
  68. console.warn( 'Use .addVectors( a, b ) instead.' );
  69. return this.addVectors( v, w );
  70. }
  71. this.x += v.x;
  72. this.y += v.y;
  73. this.z += v.z;
  74. this.w += v.w;
  75. return this;
  76. };
  77. Point.prototype.addVectors = function( a, b ) {
  78. this.x = a.x + b.x;
  79. this.y = a.y + b.y;
  80. this.z = a.z + b.z;
  81. this.w = a.w + b.w;
  82. return this;
  83. };
  84. Point.prototype.sub = function( v, w ) {
  85. if ( w !== undefined ) {
  86. console.warn( 'Use .subVectors( a, b ) instead.' );
  87. return this.subVectors( v, w );
  88. }
  89. this.x -= v.x;
  90. this.y -= v.y;
  91. this.z -= v.z;
  92. this.w -= v.w;
  93. return this;
  94. };
  95. Point.prototype.subVectors = function( a, b ) {
  96. this.x = a.x - b.x;
  97. this.y = a.y - b.y;
  98. this.z = a.z - b.z;
  99. this.w = a.w - b.w;
  100. return this;
  101. };
  102. Point.prototype.lengthSq = function() {
  103. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  104. };
  105. Point.prototype.length = function() {
  106. return Math.sqrt( this.lengthSq() );
  107. };
  108. Point.prototype.normalize = function() {
  109. return this.divideScalar( this.length() );
  110. };
  111. Point.prototype.divideScalar = function( scalar ) {
  112. return this.multiplyScalar( 1 / scalar );
  113. };
  114. Point.prototype.distanceTo = function( v ) {
  115. return Math.sqrt( this.distanceToSquared( v ) );
  116. };
  117. Point.prototype.distanceToSquared = function( v ) {
  118. const dx = this.x - v.x;
  119. const dy = this.y - v.y;
  120. const dz = this.z - v.z;
  121. return dx * dx + dy * dy + dz * dz;
  122. };
  123. Point.prototype.multiplyScalar = function( scalar ) {
  124. if ( isFinite( scalar ) ) {
  125. this.x *= scalar;
  126. this.y *= scalar;
  127. this.z *= scalar;
  128. this.w *= scalar;
  129. } else {
  130. this.x = 0;
  131. this.y = 0;
  132. this.z = 0;
  133. this.w = 0;
  134. }
  135. return this;
  136. };
  137. Point.prototype.cross = function(v, w) {
  138. if (w !== undefined) {
  139. console.warn('Use .crossVectors( a, b ) instead.');
  140. return this.crossVectors(v, w);
  141. }
  142. const x = this.x;
  143. const y = this.y;
  144. const z = this.z;
  145. this.x = y * v.z - z * v.y;
  146. this.y = z * v.x - x * v.z;
  147. this.z = x * v.y - y * v.x;
  148. return this;
  149. };
  150. Point.prototype.crossVectors = function(a, b) {
  151. const ax = a.x;
  152. const ay = a.y;
  153. const az = a.z;
  154. const bx = b.x;
  155. const by = b.y;
  156. const bz = b.z;
  157. this.x = ay * bz - az * by;
  158. this.y = az * bx - ax * bz;
  159. this.z = ax * by - ay * bx;
  160. return this;
  161. };
  162. export {Point};