Vector2.d.ts 10 KB

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