-
Notifications
You must be signed in to change notification settings - Fork 2
/
avec2.js
251 lines (227 loc) · 5.69 KB
/
avec2.js
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
/** @module avec2 */
import * as vec2 from "./vec2.js";
const TEMP_VEC2 = vec2.create();
/**
* Sets a vector components.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {number} x
* @param {number} y
*/
export function set2(a, i, x, y) {
a[i * 2] = x;
a[i * 2 + 1] = y;
}
/**
* Sets a vector to another vector.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
*/
export function set(a, i, b, j) {
a[i * 2] = b[j * 2];
a[i * 2 + 1] = b[j * 2 + 1];
}
/**
* Compares two vectors.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @returns {boolean}
*/
export function equals(a, i, b, j) {
return a[i * 2] === b[j * 2] && a[i * 2 + 1] === b[j * 2 + 1];
}
/**
* Adds a vector to another.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
*/
export function add(a, i, b, j) {
a[i * 2] += b[j * 2];
a[i * 2 + 1] += b[j * 2 + 1];
}
/**
* Subtracts a vector from another.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
*/
export function sub(a, i, b, j) {
a[i * 2] -= b[j * 2];
a[i * 2 + 1] -= b[j * 2 + 1];
}
/**
* Scales a vector by a number.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {number} s
*/
export function scale(a, i, s) {
a[i * 2] *= s;
a[i * 2 + 1] *= s;
}
/**
* Adds two vectors after scaling the second one.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @param {number} s
*/
export function addScaled(a, i, b, j, s) {
a[i * 2] += b[j * 2] * s;
a[i * 2 + 1] += b[j * 2 + 1] * s;
}
/**
* Calculates the dot product of two vectors.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @returns {number}
*/
export function dot(a, i, b, j) {
return a[i * 2] * b[j * 2] + a[i * 2 + 1] * b[j * 2 + 1];
}
/**
* Calculates the length of a vector.
* @param {import("./types.js").avec2} a
* @param {number} i
* @returns {number}
*/
export function length(a, i) {
const x = a[i * 2];
const y = a[i * 2 + 1];
return Math.sqrt(x * x + y * y);
}
/**
* Calculates the squared length of a vector.
* @param {import("./types.js").avec2} a
* @param {number} i
* @returns {number}
*/
export function lengthSq(a, i) {
const x = a[i * 2];
const y = a[i * 2 + 1];
return x * x + y * y;
}
/**
* Normalises a vector.
* @param {import("./types.js").avec2} a
* @param {number} i
*/
export function normalize(a, i) {
const lenSq = a[i * 2] * a[i * 2] + a[i * 2 + 1] * a[i * 2 + 1];
if (lenSq > 0) {
const len = Math.sqrt(lenSq);
a[i * 2] /= len;
a[i * 2 + 1] /= len;
}
}
/**
* Calculates the distance between two vectors.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @returns {number}
*/
export function distance(a, i, b, j) {
const dx = b[j * 2] - a[i * 2];
const dy = b[j * 2 + 1] - a[i * 2 + 1];
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Calculates the squared distance between two vectors.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @returns {number}
*/
export function distanceSq(a, i, b, j) {
const dx = b[j * 2] - a[i * 2];
const dy = b[j * 2 + 1] - a[i * 2 + 1];
return dx * dx + dy * dy;
}
/**
* Limits a vector to a length.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {number} len
*/
export function limit(a, i, len) {
const x = a[i * 2];
const y = a[i * 2 + 1];
const dsq = x * x + y * y;
const lsq = len * len;
if (lsq > 0 && dsq > lsq) {
const nd = len / Math.sqrt(dsq);
a[i * 2] *= nd;
a[i * 2 + 1] *= nd;
}
}
/**
* Linearly interpolates between two vectors.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {import("./types.js").avec2} b
* @param {number} j
* @param {number} t
*/
export function lerp(a, i, b, j, t) {
const x = a[i * 2];
const y = a[i * 2 + 1];
a[i * 2] = x + (b[j * 2] - x) * t;
a[i * 2 + 1] = y + (b[j * 2 + 1] - y) * t;
}
/**
* Executes a function once for each array element.
* @param {import("./types.js").avec2} a
* @param {import("./types.js").iterativeCallback} callbackFn
*/
export function forEach(a, callbackFn) {
for (let i = 0; i < a.length / 2; i++) {
TEMP_VEC2[0] = a[i * 2];
TEMP_VEC2[1] = a[i * 2 + 1];
callbackFn(TEMP_VEC2, i, a);
a[i * 2] = TEMP_VEC2[0];
a[i * 2 + 1] = TEMP_VEC2[1];
}
}
/**
* Creates a new array populated with the results of calling a provided function on every element in the calling array.
* @param {import("./types.js").avec2} a
* @param {import("./types.js").iterativeCallback} callbackFn
* @returns {import("./types.js").avec2}
*/
export function map(a, callbackFn) {
const b = new a.constructor(a.length);
const element = new a.constructor(2);
for (let i = 0; i < a.length / 2; i++) {
element[0] = a[i * 2];
element[1] = a[i * 2 + 1];
const returnValue = callbackFn(element, i, a);
b[i * 2] = returnValue[0];
b[i * 2 + 1] = returnValue[1];
}
return b;
}
/**
* Prints a vector to a string.
* @param {import("./types.js").avec2} a
* @param {number} i
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, i, precision = 4) {
const scale = 10 ** precision;
// prettier-ignore
return `[${Math.floor(a[i * 2] * scale) / scale}, ${Math.floor(a[i * 2 + 1] * scale) / scale}]`;
}