/
Vector2.mjs
196 lines (165 loc) · 3.6 KB
/
Vector2.mjs
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
import { EPSILON } from './common.mjs';
/**
* Default values
*
* @readonly
* @memberof Vector2
* @type {Array}
*/
const DEFAULT = [0.0, 0.0];
Object.freeze(DEFAULT);
/**
* Element length
*
* @readonly
* @memberof Vector2
* @type {Number}
*/
const LENGTH = 2;
/**
* A 2-component vector.
*
* @extends Float32Array
*/
class Vector2 extends Float32Array {
/**
* Create a new vector.
*
* @param {...*} args Arguments for new vector
*/
constructor(...args) {
if (args.length === 0) {
super(DEFAULT);
} else {
super(...args);
}
if (this.length !== LENGTH) {
throw new Error('Invalid length');
}
}
/**
* @type {Number}
*/
get x() {
return this[0];
}
set x(x) {
this[0] = x;
}
/**
* @type {Number}
*/
get y() {
return this[1];
}
set y(y) {
this[1] = y;
}
/**
* Check for approximate equality against the given vector using given
* epsilon.
*
* @param {Vector2} v Vector to compare
* @param {Number} e Epsilon
* @returns {Boolean} Approximate equality
*/
approximates(v, e = EPSILON) {
const t0 = this[0], t1 = this[1];
const v0 = v[0], v1 = v[1];
return Math.abs(t0 - v0) <= e * Math.max(1.0, Math.abs(t0), Math.abs(v0)) &&
Math.abs(t1 - v1) <= e * Math.max(1.0, Math.abs(t1), Math.abs(v1));
}
/**
* Check for exact equality against the given vector.
*
* @param {Vector2} v Vector to compare
* @returns {Boolean} Equality
*/
equals(v) {
return this[0] === v[0] && this[1] === v[1];
}
/**
* Set the elements of this vector to the given values.
*
* @param {Number} x X component
* @param {Number} y Y component
* @returns {Vector2} Self
*/
setElements(x, y) {
this[0] = x;
this[1] = y;
return this;
}
/**
* Execute the provided function once for each vector in the given array.
*
* @param {(Float32Array|Array)} arr Array to traverse
* @param {Function} cb Callback
* @returns {void}
*/
static forEach(arr, cb) {
if (arr.length % LENGTH !== 0) {
throw new Error('Invalid length');
}
const l = arr.length;
const v = new Vector2();
for (let i = 0; i < l; i += LENGTH) {
v[0] = arr[i];
v[1] = arr[i + 1];
cb.call(this, v, i / LENGTH, arr);
arr[i] = v[0];
arr[i + 1] = v[1];
}
}
/**
* Generate an iterator capable of iterating over an array as vector indices.
*
* @param {(Float32Array|Array)} arr Array to iterate
* @returns {Iterator} New iterator
*/
static *keys(arr) {
if (arr.length % LENGTH !== 0) {
throw new Error('Invalid length');
}
const l = arr.length / LENGTH;
for (let i = 0; i < l; i++) {
yield i;
}
}
/**
* Create a new vector with a variable number of arguments.
*
* @param {...*} args Arguments for new vector
* @returns {Vector2} New vector
*/
static of(...args) {
if (args.length === 0) {
return new Vector2();
} else {
return new Vector2(args);
}
}
/**
* Generate an iterator capable of iterating over an array as vectors.
*
* @param {(Float32Array|Array)} arr Array to iterate
* @returns {Iterator} New iterator
*/
static *values(arr) {
if (arr.length % LENGTH !== 0) {
throw new Error('Invalid length');
}
const l = arr.length;
const v = new Vector2();
for (let i = 0; i < l; i += LENGTH) {
v[0] = arr[i];
v[1] = arr[i + 1];
yield v;
arr[i] = v[0];
arr[i + 1] = v[1];
}
}
}
Vector2.DEFAULT = DEFAULT;
Vector2.LENGTH = LENGTH;
export default Vector2;