-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2.ts
375 lines (341 loc) · 7.66 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import mat2 from "./mat2";
import mat3 from "./mat3";
import mat4 from "./mat4";
import vec3 from "./vec3";
export default class vec2 {
/**
* 2 Dimensional Vector
* @module vec2
*/
data: Float32Array;
get x() {
return this.data[0];
}
set x(value: number) {
this.data[0] = value;
}
get y() {
return this.data[1];
}
set y(value: number) {
this.data[1] = value;
}
/**
* Creates a new, empty vec2
*
* @returns {vec2} a new 2D vector
*/
constructor(x: number = 0, y: number = 0) {
this.data = new Float32Array([x, y]);
}
/**
* Creates a new vec2 initialized with values from an existing vector
*
* @returns {vec2} a new 2D vector
*/
clone() {
return new vec2(this.data[0], this.data[1]);
}
/**
* Copy the values from one vec2 to another
*
* @param {vec2} out the receiving vector
* @param {vec2} a the source vector
* @returns {vec2} out
*/
copy(a: vec2) {
this.data.set(a.data);
return this;
}
/**
* Set the components of a vec2 to the given values
*
* @param {number} x X component
* @param {number} y Y component
* @returns {vec2} out
*/
set(x: number = 0, y: number = 0) {
this.data.set([x, y]);
return this;
}
/**
* Adds two vec2's
*
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
add(b: vec2) {
this.data[0] += b.data[0];
this.data[1] += b.data[1];
return this;
}
/**
* Subtracts vector b from vector a
*
* @param {vec2} vector the second operand
* @returns {vec2} out
*/
subtract(vector: vec2) {
const x=vector.data[0],y=vector.data[1];
this.data[0] -= x;
this.data[1] -= y;
return this;
}
/**
* Multiplies two vec2's
*
* @param {vec2} vector the second operand
* @returns {vec2} out
*/
multiply(vector: vec2) {
const x=vector.data[0],y=vector.data[1];
const data=this.data
data[0] *= x;
data[1] *= y;
return this;
}
/**
* Divides two vec2's
*
* @param {vec2} vector the second operand
* @returns {vec2} out
*/
divide(vector: vec2) {
const x=vector.data[0],y=vector.data[1];
this.data[0] /= x;
this.data[1] /= y;
return this;
}
/**
* Scales a vec2 by a scalar number
*
* @param {number} s amount to scale the vector by
* @returns {vec2} out
*/
scale(s: number) {
const data=this.data
data[0] *= s;
data[1] *= s;
return this;
}
/**
* Calculates the euclidian distance between two vec2's
* @param {vec2} vector the second operand
* @returns {number} distance between a and b
*/
distance(vector: vec2) {
const a=this.data;
const b=vector.data
return Math.hypot(b[0] - a[0], b[1] - a[1]);
}
/**
* Calculates the squared euclidian distance between two vec2's
* @param {vec2} vector the second operand
* @returns {number} squared distance between a and b
*/
squaredDistance(vector: vec2) {
const a=this.data;
const b=vector.data
var x = b[0] - a[0],
y = b[1] - a[1];
return x * x + y * y;
}
/**
* Calculates the length of a vec2
*
* @returns {number} length of a
*/
length() {
const a=this.data;
return Math.hypot(a[0], a[1]);
}
/**
* Calculates the squared length of a vec2
*
* @returns {number} squared length of a
*/
squaredLength() {
const a=this.data;
var x = a[0],
y = a[1];
return x * x + y * y;
}
/**
* Negates the components of a vec2
*
* @returns {vec2} out
*/
negate() {
const data=this.data;
data[0] = -data[0];
data[1] = -data[1];
return this;
}
/**
* Returns the inverse of the components of a vec2
*
* @returns {vec2} out
*/
inverse() {
const out=this.data;
out[0] = 1.0 / out[0];
out[1] = 1.0 / out[1];
return this;
}
/**
* Normalize a vec2
*
* @returns {vec2} out
*/
normalize() {
const out=this.data;
var x = out[0],
y = out[1];
var len = x * x + y * y;
if (len > 0) {
//TODO: evaluate use of glm_invsqrt here?
len = 1 / Math.sqrt(len);
}
out[0] = out[0] * len;
out[1] = out[1] * len;
return this;
}
/**
* Calculates the dot product of two vec2's
*
* @param {vec2} b the second operand
* @returns {number} dot product of a and b
*/
dot(vector: vec2) {
const a=this.data;
const b=vector.data
return a[0] * b[0] + a[1] * b[1];
}
/**
* Computes the cross product of two vec2's
* Note that the cross product must by definition produce a 3D vector
* @param {vec2} vector the second operand
* @returns {vec3} out
*/
cross(vector: vec2) {
const a=this.data;
const b=vector.data
var z = a[0] * b[1] - a[1] * b[0];
return new vec3(0, 0, z);
}
/**
* Performs a linear interpolation between two vec2's
*
* @param {vec2} vector the second operand
* @param {number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec2} out
*/
lerp(vector: vec2, t: number) {
const a=this.data;
const b=vector.data
var ax = a[0],
ay = a[1];
return this.set(
ax + t * (b[0] - ax),
ay + t * (b[1] - ay)
);
}
/**
* Transforms the vec2 with a mat2
* @param {ReadonlyMat2} matrix matrix to transform with
* @returns {vec2} out
*/
transformMat2(matrix: mat2) {
const data=this.data
const m=matrix.data;
var x = data[0],
y = data[1];
return this.set(
m[0] * x + m[2] * y,
m[1] * x + m[3] * y
);
}
/**
* Transforms the vec2 with a mat3
* 3rd vector component is implicitly '1'
*
* @param {mat3} matrix matrix to transform with
* @returns {vec2} out
*/
transformMat3(matrix: mat3) {
const a=this.data;
const m=matrix.data;
var x = a[0],
y = a[1];
return this.set(
m[0] * x + m[3] * y + m[6],
m[1] * x + m[4] * y + m[7]
);
}
/**
* Transforms the vec2 with a mat4
* 3rd vector component is implicitly '0'
* 4th vector component is implicitly '1'
* @param {mat4} matrix matrix to transform with
* @returns {vec2} out
*/
transformMat4(matrix: mat4) {
const a=this.data;
const m=matrix.data;
let x = a[0];
let y = a[1];
return this.set(
m[0] * x + m[4] * y + m[12],
m[1] * x + m[5] * y + m[13]
);
}
/**
* Rotate a 2D vector
* @param {vec2} origin The origin of the rotation
* @param {number} rad The angle of rotation in radians
* @returns {vec2} out
*/
rotate(origin: vec2, rad: number) {
const a=this.data;
const b=origin.data;
//Translate point to the origin
let p0 = a[0] - b[0],
p1 = a[1] - b[1],
sinC = Math.sin(rad),
cosC = Math.cos(rad);
//perform rotation and translate to correct position
return this.set(
p0 * cosC - p1 * sinC + b[0],
p0 * sinC + p1 * cosC + b[1]
);
}
/**
* Get the angle between two 2D vectors
* @param {vec2} vector The second operand
* @returns {number} The angle in radians
*/
angle(vector: vec2) {
const a=this.data;
const b=vector.data
let x1 = a[0],
y1 = a[1],
x2 = b[0],
y2 = b[1],
// mag is the product of the magnitudes of a and b
mag = Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)),
// mag &&.. short circuits if mag == 0
cosine = mag && (x1 * x2 + y1 * y2) / mag;
// Math.min(Math.max(cosine, -1), 1) clamps the cosine between -1 and 1
return Math.acos(Math.min(Math.max(cosine, -1), 1));
}
/**
* Set the components of a vec2 to zero
*
* @returns {vec2} out
*/
zero() {
this.data.fill(0);
return this;
}
}