Repository navigation
Expand file tree
/
Copy path_PVector.js
More file actions
261 lines (243 loc) · 9.04 KB
/
Copy path_PVector.js
File metadata and controls
261 lines (243 loc) · 9.04 KB
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
module.exports = function(options, undef) {
"use strict";
const TAU = 2 * Math.PI,
lerp = (start, stop, amt) => +start + amt*(stop - start),
sq = n => n*n,
pjsCheck = obj => obj != null && 'noLoop' in obj,
argsErr = (mtd, len, min) => {
throw `Too few args passed to ${mtd}() [${len} < ${min}].`;
};
function _PVector(x, y, z) {
this.x = x || 0, this.y = y || 0, this.z = z || 0;
}
_PVector.fromAngle = (ang, t) =>
t? t.set(Math.cos(ang), Math.sin(ang))
: new _PVector(Math.cos(ang), Math.sin(ang));
_PVector.random2D = (t, p) => {
const isPjs = pjsCheck(t), rnd = p? p : isPjs && t || Math;
return _PVector.fromAngle(TAU * rnd.random(), !isPjs && t || void 0);
};
_PVector.random3D = (t, p) => {
const isPjs = pjsCheck(t),
rnd = p? p : isPjs && t || Math,
ang = TAU * rnd.random(),
vz = 2*rnd.random() - 1,
vzr = Math.sqrt(1 - vz*vz),
vx = vzr * Math.cos(ang),
vy = vzr * Math.sin(ang);
return t && !isPjs? t.set(vx, vy, vz) : new _PVector(vx, vy, vz);
};
_PVector.dist = (v1, v2) => Math.sqrt(_PVector.distSq(v1, v2));
_PVector.distSq = (v1, v2) => sq(v1.x-v2.x) + sq(v1.y-v2.y) + sq(v1.z-v2.z);
_PVector.dot = (v1, v2) => v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
_PVector.cross = (v1, v2, t) => {
const cx = v1.y*v2.z - v2.y*v1.z,
cy = v1.z*v2.x - v2.z*v1.x,
cz = v1.x*v2.y - v2.x*v1.y;
return t && t.set(cx, cy, cz) || new _PVector(cx, cy, cz);
};
_PVector.angleBetween = (v1, v2) => {
if (!v1.x && !v1.y && !v1.z || !v2.x && !v2.y && !v2.z) return 0;
const amt = _PVector.dot(v1, v2) / Math.sqrt(v1.magSq() * v2.magSq());
return amt <= -1? Math.PI : amt >= 1? 0 : Math.acos(amt);
};
_PVector.lerp = (v1, v2, amt, t) =>
(t && t.set(v1) || v1.copy()).lerp(v2, amt);
_PVector.add = (v1, v2, t) =>
t? t.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
: new _PVector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
_PVector.sub = (v1, v2, t) =>
t? t.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
: new _PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
_PVector.mult = (v, n, t) => {
if (typeof n === 'object')
return t? t.set(v.x*n.x, v.y*n.y, v.z*n.z)
: new _PVector(v.x*n.x, v.y*n.y, v.z*n.z);
else
return t? t.set(v.x*n, v.y*n, v.z*n)
: new _PVector(v.x*n, v.y*n, v.z*n);
};
_PVector.div = (v, n, t) => {
if (typeof n === 'object')
return t? t.set(v.x/n.x, v.y/n.y, v.z/n.z)
: new _PVector(v.x/n.x, v.y/n.y, v.z/n.z);
else
return t? t.set (v.x/n, v.y/n, v.z/n)
: new _PVector(v.x/n, v.y/n, v.z/n);
};
_PVector.mod = (v, n, t) => {
if (typeof n === 'object')
return t? t.set(v.x%n.x, v.y%n.y, v.z%n.z)
: new _PVector(v.x%n.x, v.y%n.y, v.z%n.z);
else
return t? t.set(v.x%n, v.y%n, v.z%n)
: new _PVector(v.x%n, v.y%n, v.z%n);
};
_PVector.compare = (a, b) => a.x - b.x || a.y - b.y || a.z - b.z;
_PVector.prototype = {
compareTo(v) {
return this.x - v.x || this.y - v.y || this.z - v.z;
},
array() { return [this.x, this.y, this.z]; },
object() { return { x: this.x, y: this.y, z: this.z }; },
copy() { return new _PVector(this.x, this.y, this.z); },
clone: null,
get(t) {
if (!t) return t === void 0 && this.copy() || this.array();
else if ('z' in t) t.x = this.x, t.y = this.y, t.z = this.z;
else t[0] = this.x, t[1] = this.y, t[2] = this.z;
return t;
},
set(v, y, z) {
if (y != void 0) this.x = +v, this.y = +y, z != void 0 && (this.z = +z);
else this.set(v[0] || v.x || 0, v[1] || v.y || 0, v[2] || v.z);
return this;
},
normalize(t, mag) {
const m = +mag || this.mag(), canDivide = m === m && m !== 0 && m !== 1;
if (!arguments.length) return canDivide && this.div(m) || this;
return canDivide? _PVector.div(this, m, t)
: t && t.set(this) || this.copy();
},
limit(max, t, magSq) {
const mSq = magSq || this.magSq(), overMax = mSq > max*max;
t === null && (t = new _PVector);
return !t? overMax && this.normalize().mult(max) || this
: overMax && this.normalize(t, Math.sqrt(mSq)).mult(max)
|| t.set(this);
},
heading() { return Math.atan2(this.y, this.x); },
heading2D: null, // @Deprecated
mag() { return Math.sqrt(this.magSq()); },
magSq() { return this.x*this.x + this.y*this.y + this.z*this.z; },
setMag(t, len, mag) {
return typeof t === 'object'?
this.normalize(t, mag).mult(len) : this.normalize().mult(t);
},
rotate(ang, t) {
const c = Math.cos(ang),
s = Math.sin(ang),
x = c*this.x - s*this.y,
y = s*this.x + c*this.y;
t === null && (t = new _PVector);
return (t || this).set(x, y);
},
rotateX(ang, t) {
const c = Math.cos(ang),
s = Math.sin(ang),
y = c*this.y - s*this.z,
z = s*this.y + c*this.z;
t === null && (t = new _PVector);
return (t || this).set(this.x, y, z);
},
rotateY(ang, t) {
const c = Math.cos(ang),
s = Math.sin(ang),
x = s*this.z + c*this.x,
z = c*this.z - s*this.x;
t === null && (t = new _PVector);
return (t || this).set(x, this.y, z);
},
rotateZ: null,
fromAngle(ang, t) {
return _PVector.fromAngle(ang, t || this);
},
random2D(t, p) {
return pjsCheck(t) && _PVector.random2D(this, t)
|| _PVector.random2D(t === void 0 && this || t, p);
},
random3D(t, p) {
return pjsCheck(t) && _PVector.random3D(this, t)
|| _PVector.random3D(t === void 0 && this || t, p);
},
dist(v1, v2) {
return v2? _PVector.dist(v1, v2) : _PVector.dist(this, v1);
},
distSq(v1, v2) {
return v2? _PVector.distSq(v1, v2) : _PVector.distSq(this, v1);
},
dot(v, y, z) {
return typeof v != 'object'? this.x*v + this.y*+y + this.z*z :
y == void 0? _PVector.dot(this, v) : _PVector.dot(v, y);
},
cross(v1, v2, t) {
return t && _PVector.cross(v1, v2, t) || _PVector.cross(this, v1, v2);
},
angleBetween(v) { return _PVector.angleBetween(this, v); },
lerp(a, b, c, d) {
let x, y, z, amt;
const len = arguments.length;
if ((len | 1) === 1) argsErr('lerp', len, 2);
if (len === 2) { // given vector and amt
({x, y, z} = a), amt = b;
} else if (len === 3) { // given vector 1, vector 2 and amt
return _PVector.lerp(a, b, c);
} else { // given x, y, z and amt
[x, y, z, amt] = arguments;
}
return this.set(lerp(this.x, x, amt),
lerp(this.y, y, amt),
lerp(this.z, z, amt));
},
add(v, y, z) {
if (y != void 0) {
if (typeof y === 'object') return _PVector.add(v, y, z);
this.x += +v, this.y += +y, z != void 0 && (this.z += +z);
} else if (typeof v === 'object')
this.x += v.x, this.y += v.y, this.z += v.z;
else
this.x += +v, this.y += +v, this.z += +v;
return this;
},
sub(v, y, z) {
if (y != void 0) {
if (typeof y === 'object') return _PVector.sub(v, y, z);
this.x -= v, this.y -= y, z != void 0 && (this.z -= z);
} else if (typeof v === 'object')
this.x -= v.x, this.y -= v.y, this.z -= v.z;
else
this.x -= v, this.y -= v, this.z -= v;
return this;
},
mult(v, n, t) {
if (n != void 0) return _PVector.mult(v, n, t);
if (typeof v === 'object') this.x *= v.x, this.y *= v.y, this.z *= v.z;
else this.x *= v, this.y *= v, this.z *= v;
return this;
},
div(v, n, t) {
if (n != void 0) return _PVector.div(v, n, t);
if (typeof v === 'object') this.x /= v.x, this.y /= v.y, this.z /= v.z;
else this.x /= v, this.y /= v, this.z /= v;
return this;
},
mod(v, n, t) {
if (n != void 0) return _PVector.mod(v, n, t);
if (typeof v === 'object') this.x %= v.x, this.y %= v.y, this.z %= v.z;
else this.x %= v, this.y %= v, this.z %= v;
return this;
},
negate() {
this.x *= -1, this.y *= -1, this.z *= -1;
return this;
},
clear() {
this.x = this.y = this.z = 0;
return this;
},
isNaN() {
return this.x !== this.x || this.y !== this.y || this.z !== this.z;
},
toString() { return `[ ${this.x}, ${this.y}, ${this.z} ]`; },
valueOf() { return this.x; },
hashCode() { return this.x + this.y + this.z; },
equals(o) {
return o === this? true : o instanceof _PVector &&
o.x === this.x && o.y === this.y && o.z === this.z;
}
};
_PVector.prototype.clone = _PVector.prototype.copy;
_PVector.prototype.heading2D = _PVector.prototype.heading; // @Deprecated
_PVector.prototype.rotateZ = _PVector.prototype.rotate;
return _PVector;
};