Vector2.js 5.6 KB

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