Vector2.js 5.9 KB

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