Vector2.d.ts 10 KB

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