Vector2.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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.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. clone: function () {
  41. return new this.constructor( this.x, this.y );
  42. },
  43. copy: function ( v ) {
  44. this.x = v.x;
  45. this.y = v.y;
  46. return this;
  47. },
  48. add: function ( v, w ) {
  49. if ( w !== undefined ) {
  50. console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  51. return this.addVectors( v, w );
  52. }
  53. this.x += v.x;
  54. this.y += v.y;
  55. return this;
  56. },
  57. addScalar: function ( s ) {
  58. this.x += s;
  59. this.y += s;
  60. return this;
  61. },
  62. addVectors: function ( a, b ) {
  63. this.x = a.x + b.x;
  64. this.y = a.y + b.y;
  65. return this;
  66. },
  67. addScaledVector: function ( v, s ) {
  68. this.x += v.x * s;
  69. this.y += v.y * s;
  70. return this;
  71. },
  72. sub: function ( v, w ) {
  73. if ( w !== undefined ) {
  74. console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  75. return this.subVectors( v, w );
  76. }
  77. this.x -= v.x;
  78. this.y -= v.y;
  79. return this;
  80. },
  81. subScalar: function ( s ) {
  82. this.x -= s;
  83. this.y -= s;
  84. return this;
  85. },
  86. subVectors: function ( a, b ) {
  87. this.x = a.x - b.x;
  88. this.y = a.y - b.y;
  89. return this;
  90. },
  91. multiply: function ( v ) {
  92. this.x *= v.x;
  93. this.y *= v.y;
  94. return this;
  95. },
  96. multiplyScalar: function ( s ) {
  97. this.x *= s;
  98. this.y *= s;
  99. return this;
  100. },
  101. divide: function ( v ) {
  102. this.x /= v.x;
  103. this.y /= v.y;
  104. return this;
  105. },
  106. divideScalar: function ( scalar ) {
  107. if ( scalar !== 0 ) {
  108. var invScalar = 1 / scalar;
  109. this.x *= invScalar;
  110. this.y *= invScalar;
  111. } else {
  112. this.x = 0;
  113. this.y = 0;
  114. }
  115. return this;
  116. },
  117. min: function ( v ) {
  118. if ( this.x > v.x ) {
  119. this.x = v.x;
  120. }
  121. if ( this.y > v.y ) {
  122. this.y = v.y;
  123. }
  124. return this;
  125. },
  126. max: function ( v ) {
  127. if ( this.x < v.x ) {
  128. this.x = v.x;
  129. }
  130. if ( this.y < v.y ) {
  131. this.y = v.y;
  132. }
  133. return this;
  134. },
  135. clamp: function ( min, max ) {
  136. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  137. if ( this.x < min.x ) {
  138. this.x = min.x;
  139. } else if ( this.x > max.x ) {
  140. this.x = max.x;
  141. }
  142. if ( this.y < min.y ) {
  143. this.y = min.y;
  144. } else if ( this.y > max.y ) {
  145. this.y = max.y;
  146. }
  147. return this;
  148. },
  149. clampScalar: function () {
  150. var min, max;
  151. return function clampScalar( minVal, maxVal ) {
  152. if ( min === undefined ) {
  153. min = new THREE.Vector2();
  154. max = new THREE.Vector2();
  155. }
  156. min.set( minVal, minVal );
  157. max.set( maxVal, maxVal );
  158. return this.clamp( min, max );
  159. };
  160. }(),
  161. clampLength: function ( min, max ) {
  162. var oldLength = this.length();
  163. var newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
  164. if ( newLength !== oldLength ) {
  165. this.divideScalar( oldLength ).multiplyScalar( newLength );
  166. }
  167. return this;
  168. },
  169. floor: function () {
  170. this.x = Math.floor( this.x );
  171. this.y = Math.floor( this.y );
  172. return this;
  173. },
  174. ceil: function () {
  175. this.x = Math.ceil( this.x );
  176. this.y = Math.ceil( this.y );
  177. return this;
  178. },
  179. round: function () {
  180. this.x = Math.round( this.x );
  181. this.y = Math.round( this.y );
  182. return this;
  183. },
  184. roundToZero: function () {
  185. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  186. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  187. return this;
  188. },
  189. negate: function () {
  190. this.x = - this.x;
  191. this.y = - this.y;
  192. return this;
  193. },
  194. dot: function ( v ) {
  195. return this.x * v.x + this.y * v.y;
  196. },
  197. lengthSq: function () {
  198. return this.x * this.x + this.y * this.y;
  199. },
  200. length: function () {
  201. return Math.sqrt( this.x * this.x + this.y * this.y );
  202. },
  203. lengthManhattan: function() {
  204. return Math.abs( this.x ) + Math.abs( this.y );
  205. },
  206. normalize: function () {
  207. return this.divideScalar( this.length() );
  208. },
  209. distanceTo: function ( v ) {
  210. return Math.sqrt( this.distanceToSquared( v ) );
  211. },
  212. distanceToSquared: function ( v ) {
  213. var dx = this.x - v.x, dy = this.y - v.y;
  214. return dx * dx + dy * dy;
  215. },
  216. setLength: function ( l ) {
  217. var oldLength = this.length();
  218. if ( oldLength !== 0 && l !== oldLength ) {
  219. this.multiplyScalar( l / oldLength );
  220. }
  221. return this;
  222. },
  223. lerp: function ( v, alpha ) {
  224. this.x += ( v.x - this.x ) * alpha;
  225. this.y += ( v.y - this.y ) * alpha;
  226. return this;
  227. },
  228. lerpVectors: function ( v1, v2, alpha ) {
  229. this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  230. return this;
  231. },
  232. equals: function ( v ) {
  233. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  234. },
  235. fromArray: function ( array, offset ) {
  236. if ( offset === undefined ) offset = 0;
  237. this.x = array[ offset ];
  238. this.y = array[ offset + 1 ];
  239. return this;
  240. },
  241. toArray: function ( array, offset ) {
  242. if ( array === undefined ) array = [];
  243. if ( offset === undefined ) offset = 0;
  244. array[ offset ] = this.x;
  245. array[ offset + 1 ] = this.y;
  246. return array;
  247. },
  248. fromAttribute: function ( attribute, index, offset ) {
  249. if ( offset === undefined ) offset = 0;
  250. index = index * attribute.itemSize + offset;
  251. this.x = attribute.array[ index ];
  252. this.y = attribute.array[ index + 1 ];
  253. return this;
  254. }
  255. };
粤ICP备19079148号