Vector2.js 5.7 KB

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