Vector2.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. class Vector2 {
  2. constructor( x = 0, y = 0 ) {
  3. Vector2.prototype.isVector2 = 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 ) {
  61. this.x += v.x;
  62. this.y += v.y;
  63. return this;
  64. }
  65. addScalar( s ) {
  66. this.x += s;
  67. this.y += s;
  68. return this;
  69. }
  70. addVectors( a, b ) {
  71. this.x = a.x + b.x;
  72. this.y = a.y + b.y;
  73. return this;
  74. }
  75. addScaledVector( v, s ) {
  76. this.x += v.x * s;
  77. this.y += v.y * s;
  78. return this;
  79. }
  80. sub( v ) {
  81. this.x -= v.x;
  82. this.y -= v.y;
  83. return this;
  84. }
  85. subScalar( s ) {
  86. this.x -= s;
  87. this.y -= s;
  88. return this;
  89. }
  90. subVectors( a, b ) {
  91. this.x = a.x - b.x;
  92. this.y = a.y - b.y;
  93. return this;
  94. }
  95. multiply( v ) {
  96. this.x *= v.x;
  97. this.y *= v.y;
  98. return this;
  99. }
  100. multiplyScalar( scalar ) {
  101. this.x *= scalar;
  102. this.y *= scalar;
  103. return this;
  104. }
  105. divide( v ) {
  106. this.x /= v.x;
  107. this.y /= v.y;
  108. return this;
  109. }
  110. divideScalar( scalar ) {
  111. return this.multiplyScalar( 1 / scalar );
  112. }
  113. applyMatrix3( m ) {
  114. const x = this.x, y = this.y;
  115. const e = m.elements;
  116. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
  117. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
  118. return this;
  119. }
  120. min( v ) {
  121. this.x = Math.min( this.x, v.x );
  122. this.y = Math.min( this.y, v.y );
  123. return this;
  124. }
  125. max( v ) {
  126. this.x = Math.max( this.x, v.x );
  127. this.y = Math.max( this.y, v.y );
  128. return this;
  129. }
  130. clamp( min, max ) {
  131. // assumes min < max, componentwise
  132. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  133. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  134. return this;
  135. }
  136. clampScalar( minVal, maxVal ) {
  137. this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
  138. this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
  139. return this;
  140. }
  141. clampLength( min, max ) {
  142. const length = this.length();
  143. return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
  144. }
  145. floor() {
  146. this.x = Math.floor( this.x );
  147. this.y = Math.floor( this.y );
  148. return this;
  149. }
  150. ceil() {
  151. this.x = Math.ceil( this.x );
  152. this.y = Math.ceil( this.y );
  153. return this;
  154. }
  155. round() {
  156. this.x = Math.round( this.x );
  157. this.y = Math.round( this.y );
  158. return this;
  159. }
  160. roundToZero() {
  161. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  162. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  163. return this;
  164. }
  165. negate() {
  166. this.x = - this.x;
  167. this.y = - this.y;
  168. return this;
  169. }
  170. dot( v ) {
  171. return this.x * v.x + this.y * v.y;
  172. }
  173. cross( v ) {
  174. return this.x * v.y - this.y * v.x;
  175. }
  176. lengthSq() {
  177. return this.x * this.x + this.y * this.y;
  178. }
  179. length() {
  180. return Math.sqrt( this.x * this.x + this.y * this.y );
  181. }
  182. manhattanLength() {
  183. return Math.abs( this.x ) + Math.abs( this.y );
  184. }
  185. normalize() {
  186. return this.divideScalar( this.length() || 1 );
  187. }
  188. angle() {
  189. // computes the angle in radians with respect to the positive x-axis
  190. const angle = Math.atan2( - this.y, - this.x ) + Math.PI;
  191. return angle;
  192. }
  193. distanceTo( v ) {
  194. return Math.sqrt( this.distanceToSquared( v ) );
  195. }
  196. distanceToSquared( v ) {
  197. const dx = this.x - v.x, dy = this.y - v.y;
  198. return dx * dx + dy * dy;
  199. }
  200. manhattanDistanceTo( v ) {
  201. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
  202. }
  203. setLength( length ) {
  204. return this.normalize().multiplyScalar( length );
  205. }
  206. lerp( v, alpha ) {
  207. this.x += ( v.x - this.x ) * alpha;
  208. this.y += ( v.y - this.y ) * alpha;
  209. return this;
  210. }
  211. lerpVectors( v1, v2, alpha ) {
  212. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  213. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  214. return this;
  215. }
  216. equals( v ) {
  217. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  218. }
  219. fromArray( array, offset = 0 ) {
  220. this.x = array[ offset ];
  221. this.y = array[ offset + 1 ];
  222. return this;
  223. }
  224. toArray( array = [], offset = 0 ) {
  225. array[ offset ] = this.x;
  226. array[ offset + 1 ] = this.y;
  227. return array;
  228. }
  229. fromBufferAttribute( attribute, index ) {
  230. this.x = attribute.getX( index );
  231. this.y = attribute.getY( index );
  232. return this;
  233. }
  234. rotateAround( center, angle ) {
  235. const c = Math.cos( angle ), s = Math.sin( angle );
  236. const x = this.x - center.x;
  237. const y = this.y - center.y;
  238. this.x = x * c - y * s + center.x;
  239. this.y = x * s + y * c + center.y;
  240. return this;
  241. }
  242. random() {
  243. this.x = Math.random();
  244. this.y = Math.random();
  245. return this;
  246. }
  247. *[ Symbol.iterator ]() {
  248. yield this.x;
  249. yield this.y;
  250. }
  251. }
  252. export { Vector2 };
粤ICP备19079148号