Vector2.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author philogb / http://blog.thejit.org/
  4. * @author egraether / http://egraether.com/
  5. * @author zz85 / http://www.lab4games.net/zz85/blog
  6. */
  7. THREE.Vector2 = function ( x, y ) {
  8. this.x = x || 0;
  9. this.y = y || 0;
  10. };
  11. THREE.extend( THREE.Vector2.prototype, {
  12. constructor: THREE.Vector2,
  13. set: function ( x, y ) {
  14. this.x = x;
  15. this.y = y;
  16. return this;
  17. },
  18. setX: function ( x ) {
  19. this.x = x;
  20. return this;
  21. },
  22. setY: function ( y ) {
  23. this.y = y;
  24. return this;
  25. },
  26. setComponent: function ( index, value ) {
  27. switch ( index ) {
  28. case 0: this.x = value; break;
  29. case 1: this.y = value; break;
  30. default: throw new Error( "index is out of range: " + index );
  31. }
  32. },
  33. getComponent: function ( index ) {
  34. switch ( index ) {
  35. case 0: return this.x;
  36. case 1: return this.y;
  37. default: throw new Error( "index is out of range: " + index );
  38. }
  39. },
  40. copy: function ( v ) {
  41. this.x = v.x;
  42. this.y = v.y;
  43. return this;
  44. },
  45. add: function ( v, w ) {
  46. if ( w !== undefined ) {
  47. console.warn( 'DEPRECATED: Vector2\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  48. return this.addVectors( v, w );
  49. }
  50. this.x += v.x;
  51. this.y += v.y;
  52. return this;
  53. },
  54. addVectors: function ( a, b ) {
  55. this.x = a.x + b.x;
  56. this.y = a.y + b.y;
  57. return this;
  58. },
  59. addScalar: function ( s ) {
  60. this.x += s;
  61. this.y += s;
  62. return this;
  63. },
  64. sub: function ( v, w ) {
  65. if ( w !== undefined ) {
  66. console.warn( 'DEPRECATED: Vector2\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  67. return this.subVectors( v, w );
  68. }
  69. this.x -= v.x;
  70. this.y -= v.y;
  71. return this;
  72. },
  73. subVectors: function ( a, b ) {
  74. this.x = a.x - b.x;
  75. this.y = a.y - b.y;
  76. return this;
  77. },
  78. multiplyScalar: function ( s ) {
  79. this.x *= s;
  80. this.y *= s;
  81. return this;
  82. },
  83. divideScalar: function ( s ) {
  84. if ( s !== 0 ) {
  85. this.x /= s;
  86. this.y /= s;
  87. } else {
  88. this.set( 0, 0 );
  89. }
  90. return this;
  91. },
  92. min: function ( v ) {
  93. if ( this.x > v.x ) {
  94. this.x = v.x;
  95. }
  96. if ( this.y > v.y ) {
  97. this.y = v.y;
  98. }
  99. return this;
  100. },
  101. max: function ( v ) {
  102. if ( this.x < v.x ) {
  103. this.x = v.x;
  104. }
  105. if ( this.y < v.y ) {
  106. this.y = v.y;
  107. }
  108. return this;
  109. },
  110. clamp: function ( min, max ) {
  111. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  112. if ( this.x < min.x ) {
  113. this.x = min.x;
  114. } else if ( this.x > max.x ) {
  115. this.x = max.x;
  116. }
  117. if ( this.y < min.y ) {
  118. this.y = min.y;
  119. } else if ( this.y > max.y ) {
  120. this.y = max.y;
  121. }
  122. return this;
  123. },
  124. negate: function() {
  125. return this.multiplyScalar( - 1 );
  126. },
  127. dot: function ( v ) {
  128. return this.x * v.x + this.y * v.y;
  129. },
  130. lengthSq: function () {
  131. return this.x * this.x + this.y * this.y;
  132. },
  133. length: function () {
  134. return Math.sqrt( this.x * this.x + this.y * this.y );
  135. },
  136. normalize: function () {
  137. return this.divideScalar( this.length() );
  138. },
  139. distanceTo: function ( v ) {
  140. return Math.sqrt( this.distanceToSquared( v ) );
  141. },
  142. distanceToSquared: function ( v ) {
  143. var dx = this.x - v.x, dy = this.y - v.y;
  144. return dx * dx + dy * dy;
  145. },
  146. setLength: function ( l ) {
  147. var oldLength = this.length();
  148. if ( oldLength !== 0 && l !== oldLength ) {
  149. this.multiplyScalar( l / oldLength );
  150. }
  151. return this;
  152. },
  153. lerp: function ( v, alpha ) {
  154. this.x += ( v.x - this.x ) * alpha;
  155. this.y += ( v.y - this.y ) * alpha;
  156. return this;
  157. },
  158. equals: function( v ) {
  159. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  160. },
  161. clone: function () {
  162. return new THREE.Vector2( this.x, this.y );
  163. }
  164. } );
粤ICP备19079148号