Vector2.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. function Vector2( x, y ) {
  8. this.x = x || 0;
  9. this.y = y || 0;
  10. }
  11. Vector2.prototype = {
  12. constructor: Vector2,
  13. isVector2: true,
  14. get width() {
  15. return this.x;
  16. },
  17. set width( value ) {
  18. this.x = value;
  19. },
  20. get height() {
  21. return this.y;
  22. },
  23. set height( value ) {
  24. this.y = value;
  25. },
  26. //
  27. set: function ( x, y ) {
  28. this.x = x;
  29. this.y = y;
  30. return this;
  31. },
  32. setScalar: function ( scalar ) {
  33. this.x = scalar;
  34. this.y = scalar;
  35. return this;
  36. },
  37. setX: function ( x ) {
  38. this.x = x;
  39. return this;
  40. },
  41. setY: function ( y ) {
  42. this.y = y;
  43. return this;
  44. },
  45. setComponent: function ( index, value ) {
  46. switch ( index ) {
  47. case 0: this.x = value; break;
  48. case 1: this.y = value; break;
  49. default: throw new Error( 'index is out of range: ' + index );
  50. }
  51. return this;
  52. },
  53. getComponent: function ( index ) {
  54. switch ( index ) {
  55. case 0: return this.x;
  56. case 1: return this.y;
  57. default: throw new Error( 'index is out of range: ' + index );
  58. }
  59. },
  60. clone: function () {
  61. return new this.constructor( this.x, this.y );
  62. },
  63. copy: function ( v ) {
  64. this.x = v.x;
  65. this.y = v.y;
  66. return this;
  67. },
  68. add: function ( v, w ) {
  69. if ( w !== undefined ) {
  70. console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  71. return this.addVectors( v, w );
  72. }
  73. this.x += v.x;
  74. this.y += v.y;
  75. return this;
  76. },
  77. addScalar: function ( s ) {
  78. this.x += s;
  79. this.y += s;
  80. return this;
  81. },
  82. addVectors: function ( a, b ) {
  83. this.x = a.x + b.x;
  84. this.y = a.y + b.y;
  85. return this;
  86. },
  87. addScaledVector: function ( v, s ) {
  88. this.x += v.x * s;
  89. this.y += v.y * s;
  90. return this;
  91. },
  92. sub: function ( v, w ) {
  93. if ( w !== undefined ) {
  94. console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  95. return this.subVectors( v, w );
  96. }
  97. this.x -= v.x;
  98. this.y -= v.y;
  99. return this;
  100. },
  101. subScalar: function ( s ) {
  102. this.x -= s;
  103. this.y -= s;
  104. return this;
  105. },
  106. subVectors: function ( a, b ) {
  107. this.x = a.x - b.x;
  108. this.y = a.y - b.y;
  109. return this;
  110. },
  111. multiply: function ( v ) {
  112. this.x *= v.x;
  113. this.y *= v.y;
  114. return this;
  115. },
  116. multiplyScalar: function ( scalar ) {
  117. if ( isFinite( scalar ) ) {
  118. this.x *= scalar;
  119. this.y *= scalar;
  120. } else {
  121. this.x = 0;
  122. this.y = 0;
  123. }
  124. return this;
  125. },
  126. divide: function ( v ) {
  127. this.x /= v.x;
  128. this.y /= v.y;
  129. return this;
  130. },
  131. divideScalar: function ( scalar ) {
  132. return this.multiplyScalar( 1 / scalar );
  133. },
  134. min: function ( v ) {
  135. this.x = Math.min( this.x, v.x );
  136. this.y = Math.min( this.y, v.y );
  137. return this;
  138. },
  139. max: function ( v ) {
  140. this.x = Math.max( this.x, v.x );
  141. this.y = Math.max( this.y, v.y );
  142. return this;
  143. },
  144. clamp: function ( min, max ) {
  145. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  146. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  147. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  148. return this;
  149. },
  150. clampScalar: function () {
  151. var min, max;
  152. return function clampScalar( minVal, maxVal ) {
  153. if ( min === undefined ) {
  154. min = new Vector2();
  155. max = new Vector2();
  156. }
  157. min.set( minVal, minVal );
  158. max.set( maxVal, maxVal );
  159. return this.clamp( min, max );
  160. };
  161. }(),
  162. clampLength: function ( min, max ) {
  163. var length = this.length();
  164. return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
  165. },
  166. floor: function () {
  167. this.x = Math.floor( this.x );
  168. this.y = Math.floor( this.y );
  169. return this;
  170. },
  171. ceil: function () {
  172. this.x = Math.ceil( this.x );
  173. this.y = Math.ceil( this.y );
  174. return this;
  175. },
  176. round: function () {
  177. this.x = Math.round( this.x );
  178. this.y = Math.round( this.y );
  179. return this;
  180. },
  181. roundToZero: function () {
  182. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  183. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  184. return this;
  185. },
  186. negate: function () {
  187. this.x = - this.x;
  188. this.y = - this.y;
  189. return this;
  190. },
  191. dot: function ( v ) {
  192. return this.x * v.x + this.y * v.y;
  193. },
  194. lengthSq: function () {
  195. return this.x * this.x + this.y * this.y;
  196. },
  197. length: function () {
  198. return Math.sqrt( this.x * this.x + this.y * this.y );
  199. },
  200. lengthManhattan: function() {
  201. return Math.abs( this.x ) + Math.abs( this.y );
  202. },
  203. normalize: function () {
  204. return this.divideScalar( this.length() );
  205. },
  206. angle: function () {
  207. // computes the angle in radians with respect to the positive x-axis
  208. var angle = Math.atan2( this.y, this.x );
  209. if ( angle < 0 ) angle += 2 * Math.PI;
  210. return angle;
  211. },
  212. distanceTo: function ( v ) {
  213. return Math.sqrt( this.distanceToSquared( v ) );
  214. },
  215. distanceToSquared: function ( v ) {
  216. var dx = this.x - v.x, dy = this.y - v.y;
  217. return dx * dx + dy * dy;
  218. },
  219. distanceToManhattan: function ( v ) {
  220. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
  221. },
  222. setLength: function ( length ) {
  223. return this.multiplyScalar( length / this.length() );
  224. },
  225. lerp: function ( v, alpha ) {
  226. this.x += ( v.x - this.x ) * alpha;
  227. this.y += ( v.y - this.y ) * alpha;
  228. return this;
  229. },
  230. lerpVectors: function ( v1, v2, alpha ) {
  231. return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  232. },
  233. equals: function ( v ) {
  234. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  235. },
  236. fromArray: function ( array, offset ) {
  237. if ( offset === undefined ) offset = 0;
  238. this.x = array[ offset ];
  239. this.y = array[ offset + 1 ];
  240. return this;
  241. },
  242. toArray: function ( array, offset ) {
  243. if ( array === undefined ) array = [];
  244. if ( offset === undefined ) offset = 0;
  245. array[ offset ] = this.x;
  246. array[ offset + 1 ] = this.y;
  247. return array;
  248. },
  249. fromAttribute: function ( attribute, index ) {
  250. this.x = attribute.getX( index );
  251. this.y = attribute.getY( index );
  252. return this;
  253. },
  254. rotateAround: function ( center, angle ) {
  255. var c = Math.cos( angle ), s = Math.sin( angle );
  256. var x = this.x - center.x;
  257. var y = this.y - center.y;
  258. this.x = x * c - y * s + center.x;
  259. this.y = x * s + y * c + center.y;
  260. return this;
  261. }
  262. };
  263. export { Vector2 };