Vector2.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import { Matrix3 } from './Matrix3';
  2. import { BufferAttribute } from './../core/BufferAttribute';
  3. type Vector2tuple = [number, number];
  4. /**
  5. * ( interface Vector<T> )
  6. *
  7. * Abstract interface of {@link https://github.com/mrdoob/three.js/blob/master/src/math/Vector2.js|Vector2},
  8. * {@link https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js|Vector3}
  9. * and {@link https://github.com/mrdoob/three.js/blob/master/src/math/Vector4.js|Vector4}.
  10. *
  11. * Currently the members of Vector is NOT type safe because it accepts different typed vectors.
  12. *
  13. * Those definitions will be changed when TypeScript innovates Generics to be type safe.
  14. *
  15. * @example
  16. * const v:THREE.Vector = new THREE.Vector3();
  17. * v.addVectors(new THREE.Vector2(0, 1), new THREE.Vector2(2, 3)); // invalid but compiled successfully
  18. */
  19. export interface Vector {
  20. setComponent( index: number, value: number ): this;
  21. getComponent( index: number ): number;
  22. set( ...args: number[] ): this;
  23. setScalar( scalar: number ): this;
  24. /**
  25. * copy(v:T):T;
  26. */
  27. copy( v: Vector ): this;
  28. /**
  29. * NOTE: The second argument is deprecated.
  30. *
  31. * add(v:T):T;
  32. */
  33. add( v: Vector ): this;
  34. /**
  35. * addVectors(a:T, b:T):T;
  36. */
  37. addVectors( a: Vector, b: Vector ): this;
  38. addScaledVector( vector: Vector, scale: number ): this;
  39. /**
  40. * Adds the scalar value s to this vector's values.
  41. */
  42. addScalar( scalar: number ): this;
  43. /**
  44. * sub(v:T):T;
  45. */
  46. sub( v: Vector ): this;
  47. /**
  48. * subVectors(a:T, b:T):T;
  49. */
  50. subVectors( a: Vector, b: Vector ): this;
  51. /**
  52. * multiplyScalar(s:number):T;
  53. */
  54. multiplyScalar( s: number ): this;
  55. /**
  56. * divideScalar(s:number):T;
  57. */
  58. divideScalar( s: number ): this;
  59. /**
  60. * negate():T;
  61. */
  62. negate(): this;
  63. /**
  64. * dot(v:T):T;
  65. */
  66. dot( v: Vector ): number;
  67. /**
  68. * lengthSq():number;
  69. */
  70. lengthSq(): number;
  71. /**
  72. * length():number;
  73. */
  74. length(): number;
  75. /**
  76. * normalize():T;
  77. */
  78. normalize(): this;
  79. /**
  80. * NOTE: Vector4 doesn't have the property.
  81. *
  82. * distanceTo(v:T):number;
  83. */
  84. distanceTo?( v: Vector ): number;
  85. /**
  86. * NOTE: Vector4 doesn't have the property.
  87. *
  88. * distanceToSquared(v:T):number;
  89. */
  90. distanceToSquared?( v: Vector ): number;
  91. /**
  92. * setLength(l:number):T;
  93. */
  94. setLength( l: number ): this;
  95. /**
  96. * lerp(v:T, alpha:number):T;
  97. */
  98. lerp( v: Vector, alpha: number ): this;
  99. /**
  100. * equals(v:T):boolean;
  101. */
  102. equals( v: Vector ): boolean;
  103. /**
  104. * clone():T;
  105. */
  106. clone(): this;
  107. }
  108. /**
  109. * 2D vector.
  110. *
  111. * ( class Vector2 implements Vector<Vector2> )
  112. */
  113. export class Vector2 implements Vector {
  114. constructor( x?: number, y?: number );
  115. /**
  116. * @default 0
  117. */
  118. x: number;
  119. /**
  120. * @default 0
  121. */
  122. y: number;
  123. width: number;
  124. height: number;
  125. readonly isVector2: true;
  126. /**
  127. * Sets value of this vector.
  128. */
  129. set( x: number, y: number ): this;
  130. /**
  131. * Sets the x and y values of this vector both equal to scalar.
  132. */
  133. setScalar( scalar: number ): this;
  134. /**
  135. * Sets X component of this vector.
  136. */
  137. setX( x: number ): this;
  138. /**
  139. * Sets Y component of this vector.
  140. */
  141. setY( y: number ): this;
  142. /**
  143. * Sets a component of this vector.
  144. */
  145. setComponent( index: number, value: number ): this;
  146. /**
  147. * Gets a component of this vector.
  148. */
  149. getComponent( index: number ): number;
  150. /**
  151. * Returns a new Vector2 instance with the same `x` and `y` values.
  152. */
  153. clone(): this;
  154. /**
  155. * Copies value of v to this vector.
  156. */
  157. copy( v: Vector2 ): this;
  158. /**
  159. * Adds v to this vector.
  160. */
  161. add( v: Vector2, w?: Vector2 ): this;
  162. /**
  163. * Adds the scalar value s to this vector's x and y values.
  164. */
  165. addScalar( s: number ): this;
  166. /**
  167. * Sets this vector to a + b.
  168. */
  169. addVectors( a: Vector2, b: Vector2 ): this;
  170. /**
  171. * Adds the multiple of v and s to this vector.
  172. */
  173. addScaledVector( v: Vector2, s: number ): this;
  174. /**
  175. * Subtracts v from this vector.
  176. */
  177. sub( v: Vector2 ): this;
  178. /**
  179. * Subtracts s from this vector's x and y components.
  180. */
  181. subScalar( s: number ): this;
  182. /**
  183. * Sets this vector to a - b.
  184. */
  185. subVectors( a: Vector2, b: Vector2 ): this;
  186. /**
  187. * Multiplies this vector by v.
  188. */
  189. multiply( v: Vector2 ): this;
  190. /**
  191. * Multiplies this vector by scalar s.
  192. */
  193. multiplyScalar( scalar: number ): this;
  194. /**
  195. * Divides this vector by v.
  196. */
  197. divide( v: Vector2 ): this;
  198. /**
  199. * Divides this vector by scalar s.
  200. * Set vector to ( 0, 0 ) if s == 0.
  201. */
  202. divideScalar( s: number ): this;
  203. /**
  204. * Multiplies this vector (with an implicit 1 as the 3rd component) by m.
  205. */
  206. applyMatrix3( m: Matrix3 ): this;
  207. /**
  208. * If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value.
  209. */
  210. min( v: Vector2 ): this;
  211. /**
  212. * If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value.
  213. */
  214. max( v: Vector2 ): this;
  215. /**
  216. * If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value.
  217. * If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value.
  218. * @param min the minimum x and y values.
  219. * @param max the maximum x and y values in the desired range.
  220. */
  221. clamp( min: Vector2, max: Vector2 ): this;
  222. /**
  223. * If this vector's x or y values are greater than the max value, they are replaced by the max value.
  224. * If this vector's x or y values are less than the min value, they are replaced by the min value.
  225. * @param min the minimum value the components will be clamped to.
  226. * @param max the maximum value the components will be clamped to.
  227. */
  228. clampScalar( min: number, max: number ): this;
  229. /**
  230. * If this vector's length is greater than the max value, it is replaced by the max value.
  231. * If this vector's length is less than the min value, it is replaced by the min value.
  232. * @param min the minimum value the length will be clamped to.
  233. * @param max the maximum value the length will be clamped to.
  234. */
  235. clampLength( min: number, max: number ): this;
  236. /**
  237. * The components of the vector are rounded down to the nearest integer value.
  238. */
  239. floor(): this;
  240. /**
  241. * The x and y components of the vector are rounded up to the nearest integer value.
  242. */
  243. ceil(): this;
  244. /**
  245. * The components of the vector are rounded to the nearest integer value.
  246. */
  247. round(): this;
  248. /**
  249. * The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
  250. */
  251. roundToZero(): this;
  252. /**
  253. * Inverts this vector.
  254. */
  255. negate(): this;
  256. /**
  257. * Computes dot product of this vector and v.
  258. */
  259. dot( v: Vector2 ): number;
  260. /**
  261. * Computes cross product of this vector and v.
  262. */
  263. cross( v: Vector2 ): number;
  264. /**
  265. * Computes squared length of this vector.
  266. */
  267. lengthSq(): number;
  268. /**
  269. * Computes length of this vector.
  270. */
  271. length(): number;
  272. /**
  273. * @deprecated Use {@link Vector2#manhattanLength .manhattanLength()} instead.
  274. */
  275. lengthManhattan(): number;
  276. /**
  277. * Computes the Manhattan length of this vector.
  278. *
  279. * @return {number}
  280. *
  281. * @see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry}
  282. */
  283. manhattanLength(): number;
  284. /**
  285. * Normalizes this vector.
  286. */
  287. normalize(): this;
  288. /**
  289. * computes the angle in radians with respect to the positive x-axis
  290. */
  291. angle(): number;
  292. /**
  293. * Computes distance of this vector to v.
  294. */
  295. distanceTo( v: Vector2 ): number;
  296. /**
  297. * Computes squared distance of this vector to v.
  298. */
  299. distanceToSquared( v: Vector2 ): number;
  300. /**
  301. * @deprecated Use {@link Vector2#manhattanDistanceTo .manhattanDistanceTo()} instead.
  302. */
  303. distanceToManhattan( v: Vector2 ): number;
  304. /**
  305. * Computes the Manhattan length (distance) from this vector to the given vector v
  306. *
  307. * @param {Vector2} v
  308. *
  309. * @return {number}
  310. *
  311. * @see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry}
  312. */
  313. manhattanDistanceTo( v: Vector2 ): number;
  314. /**
  315. * Normalizes this vector and multiplies it by l.
  316. */
  317. setLength( length: number ): this;
  318. /**
  319. * Linearly interpolates between this vector and v, where alpha is the distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.
  320. * @param v vector to interpolate towards.
  321. * @param alpha interpolation factor in the closed interval [0, 1].
  322. */
  323. lerp( v: Vector2, alpha: number ): this;
  324. /**
  325. * Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.
  326. * @param v1 the starting vector.
  327. * @param v2 vector to interpolate towards.
  328. * @param alpha interpolation factor in the closed interval [0, 1].
  329. */
  330. lerpVectors( v1: Vector2, v2: Vector2, alpha: number ): this;
  331. /**
  332. * Checks for strict equality of this vector and v.
  333. */
  334. equals( v: Vector2 ): boolean;
  335. /**
  336. * Sets this vector's x and y value from the provided array.
  337. * @param array the source array.
  338. * @param offset (optional) offset into the array. Default is 0.
  339. */
  340. fromArray( array: number[], offset?: number ): this;
  341. /**
  342. * Sets this vector's x and y value from the provided array-like.
  343. * @param array the source array-like.
  344. * @param offset (optional) offset into the array-like. Default is 0.
  345. */
  346. fromArray( array: ArrayLike<number>, offset?: number ): this;
  347. /**
  348. * Returns an array [x, y], or copies x and y into the provided array.
  349. * @param array (optional) array to store the vector to. If this is not provided, a new array will be created.
  350. * @param offset (optional) optional offset into the array.
  351. * @return The created or provided array.
  352. */
  353. toArray( array?: number[], offset?: number ): number[];
  354. toArray( array?: Vector2tuple, offset?: 0 ): Vector2tuple;
  355. /**
  356. * Copies x and y into the provided array-like.
  357. * @param array array-like to store the vector to.
  358. * @param offset (optional) optional offset into the array.
  359. * @return The provided array-like.
  360. */
  361. toArray( array: ArrayLike<number>, offset?: number ): ArrayLike<number>;
  362. /**
  363. * Sets this vector's x and y values from the attribute.
  364. * @param attribute the source attribute.
  365. * @param index index in the attribute.
  366. */
  367. fromBufferAttribute( attribute: BufferAttribute, index: number ): this;
  368. /**
  369. * Rotates the vector around center by angle radians.
  370. * @param center the point around which to rotate.
  371. * @param angle the angle to rotate, in radians.
  372. */
  373. rotateAround( center: Vector2, angle: number ): this;
  374. /**
  375. * Sets this vector's x and y from Math.random
  376. */
  377. random(): this;
  378. }