-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVec2.ts
306 lines (262 loc) · 7.19 KB
/
Vec2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Really basic 2D vector class used for vector calculations
* @see https://github.com/mrdoob/three.js/blob/dev/src/math/Vector2.js
* @see http://glmatrix.net/docs/vec2.js.html
*/
export class Vec2 {
/** The type of the {@link Vec2} */
type: string
/** X component of our {@link Vec2} */
private _x: number
/** Y component of our {@link Vec2} */
private _y: number
/** function assigned to the {@link onChange} callback */
_onChangeCallback?(): void
/**
* Vec2 constructor
* @param x - X component of our {@link Vec2}
* @param y - Y component of our {@link Vec2}
*/
constructor(x = 0, y = x) {
this.type = 'Vec2'
this._x = x
this._y = y
}
/**
* Get the X component of the {@link Vec2}
*/
get x(): number {
return this._x
}
/**
* Set the X component of the {@link Vec2}
* Can trigger {@link onChange} callback
* @param value - X component to set
*/
set x(value: number) {
const changed = value !== this._x
this._x = value
changed && this._onChangeCallback && this._onChangeCallback()
}
/**
* Get the Y component of the {@link Vec2}
*/
get y(): number {
return this._y
}
/**
* Set the Y component of the {@link Vec2}
* Can trigger {@link onChange} callback
* @param value - Y component to set
*/
set y(value: number) {
const changed = value !== this._y
this._y = value
changed && this._onChangeCallback && this._onChangeCallback()
}
/**
* Called when at least one component of the {@link Vec2} has changed
* @param callback - callback to run when at least one component of the {@link Vec2} has changed
* @returns - our {@link Vec2}
*/
onChange(callback: () => void): Vec2 {
if (callback) {
this._onChangeCallback = callback
}
return this
}
/**
* Set the {@link Vec2} from values
* @param x - new X component to set
* @param y - new Y component to set
* @returns - this {@link Vec2} after being set
*/
set(x = 0, y = x): Vec2 {
this.x = x
this.y = y
return this
}
/**
* Add a {@link Vec2} to this {@link Vec2}
* @param vector - {@link Vec2} to add
* @returns - this {@link Vec2} after addition
*/
add(vector: Vec2 = new Vec2()): Vec2 {
this.x += vector.x
this.y += vector.y
return this
}
/**
* Add a scalar to all the components of this {@link Vec2}
* @param value - number to add
* @returns - this {@link Vec2} after addition
*/
addScalar(value = 0): Vec2 {
this.x += value
this.y += value
return this
}
/**
* Subtract a {@link Vec2} from this {@link Vec2}
* @param vector - {@link Vec2} to subtract
* @returns - this {@link Vec2} after subtraction
*/
sub(vector: Vec2 = new Vec2()): Vec2 {
this.x -= vector.x
this.y -= vector.y
return this
}
/**
* Subtract a scalar to all the components of this {@link Vec2}
* @param value - number to subtract
* @returns - this {@link Vec2} after subtraction
*/
subScalar(value = 0): Vec2 {
this.x -= value
this.y -= value
return this
}
/**
* Multiply a {@link Vec2} with this {@link Vec2}
* @param vector - {@link Vec2} to multiply with
* @returns - this {@link Vec2} after multiplication
*/
multiply(vector: Vec2 = new Vec2(1)): Vec2 {
this.x *= vector.x
this.y *= vector.y
return this
}
/**
* Multiply all components of this {@link Vec2} with a scalar
* @param value - number to multiply with
* @returns - this {@link Vec2} after multiplication
*/
multiplyScalar(value = 1): Vec2 {
this.x *= value
this.y *= value
return this
}
/**
* Divide a {@link Vec2} with this {@link Vec2}
* @param vector - {@link Vec2} to divide with
* @returns - this {@link Vec2} after division
*/
divide(vector: Vec2 = new Vec2(1)): Vec2 {
this.x /= vector.x
this.y /= vector.y
return this
}
/**
* Divide all components of this {@link Vec2} with a scalar
* @param value - number to divide with
* @returns - this {@link Vec2} after division
*/
divideScalar(value = 1): Vec2 {
this.x /= value
this.y /= value
return this
}
/**
* Copy a {@link Vec2} into this {@link Vec2}
* @param vector - {@link Vec2} to copy
* @returns - this {@link Vec2} after copy
*/
copy(vector: Vec2 = new Vec2()): Vec2 {
this.x = vector.x
this.y = vector.y
return this
}
/**
* Clone this {@link Vec2}
* @returns - cloned {@link Vec2}
*/
clone(): Vec2 {
return new Vec2(this.x, this.y)
}
/**
* Apply max values to this {@link Vec2} components
* @param vector - {@link Vec2} representing max values
* @returns - {@link Vec2} with max values applied
*/
max(vector: Vec2 = new Vec2()): Vec2 {
this.x = Math.max(this.x, vector.x)
this.y = Math.max(this.y, vector.y)
return this
}
/**
* Apply min values to this {@link Vec2} components
* @param vector - {@link Vec2} representing min values
* @returns - {@link Vec2} with min values applied
*/
min(vector: Vec2 = new Vec2()): Vec2 {
this.x = Math.min(this.x, vector.x)
this.y = Math.min(this.y, vector.y)
return this
}
/**
* Clamp this {@link Vec2} components by min and max {@link Vec2} vectors
* @param min - minimum {@link Vec2} components to compare with
* @param max - maximum {@link Vec2} components to compare with
* @returns - clamped {@link Vec2}
*/
clamp(min: Vec2 = new Vec2(), max: Vec2 = new Vec2()): Vec2 {
this.x = Math.max(min.x, Math.min(max.x, this.x))
this.y = Math.max(min.y, Math.min(max.y, this.y))
return this
}
/**
* Check if 2 {@link Vec2} are equal
* @param vector - {@link Vec2} to compare
* @returns - whether the {@link Vec2} are equals or not
*/
equals(vector: Vec2 = new Vec2()): boolean {
return this.x === vector.x && this.y === vector.y
}
/**
* Get the square length of this {@link Vec2}
* @returns - square length of this {@link Vec2}
*/
lengthSq(): number {
return this.x * this.x + this.y * this.y
}
/**
* Get the length of this {@link Vec2}
* @returns - length of this {@link Vec2}
*/
length(): number {
return Math.sqrt(this.lengthSq())
}
/**
* Normalize this {@link Vec2}
* @returns - normalized {@link Vec2}
*/
normalize(): Vec2 {
// normalize
let len = this.x * this.x + this.y * this.y
if (len > 0) {
len = 1 / Math.sqrt(len)
}
this.x *= len
this.y *= len
return this
}
/**
* Calculate the dot product of 2 {@link Vec2}
* @param vector - {@link Vec2} to use for dot product
* @returns - dot product of the 2 {@link Vec2}
*/
dot(vector: Vec2 = new Vec2()): number {
return this.x * vector.x + this.y * vector.y
}
/**
* Calculate the linear interpolation of this {@link Vec2} by given {@link Vec2} and alpha, where alpha is the percent distance along the line
* @param vector - {@link Vec2} to interpolate towards
* @param [alpha=1] - interpolation factor in the [0, 1] interval
* @returns - this {@link Vec2} after linear interpolation
*/
lerp(vector: Vec2 = new Vec2(), alpha = 1): Vec2 {
this.x += (vector.x - this.x) * alpha
this.y += (vector.y - this.y) * alpha
return this
}
}