UNPKG

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