Vector2.js 5.8 KB

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