-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
247 lines (200 loc) · 7.38 KB
/
vector.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
import vectors from 'vectors';
const add = vectors.add(3);
const copy = vectors.copy(3);
const cross = vectors.cross(3);
const dist = vectors.dist(3);
const div = vectors.div(3);
const dot = vectors.dot(3);
const mag = vectors.mag(3);
const mult = vectors.mult(3);
const normalize = vectors.normalize(3);
const sub = vectors.sub(3);
'use strict'
class Vector {
constructor(x,y,z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
}
export default vector;
Vector.prototype.globalUp = vector(0,1,0);
Vector.prototype.globalRight = vector(1,0,0);
Vector.prototype.globalDown = vector(0,0,1);
Vector.prototype.print = function() {
return `{x: ${this.x}, y: ${this.y}, z: ${this.z}}`;
}
Vector.prototype.reflect = function(normal) {
const n = normal.normalized()
// 𝑟=𝑑−2(𝑑⋅𝑛)𝑛
return this.add(n.scale(-2 * this.dot(n)))
//return this.sub(this.dot(n).scale(2).mult(n));
}
Vector.prototype.instesectsPlane = function(lineDirection, planePoint, planeNormal) {
const linePoint = this;
if (planeNormal.dot(lineDirection.normalized()) == 0) {
throw new Error(`ray and plane is paralell so no intersection ${planeNormal.string()} and ${lineDirection.string()}`);
return null;
}
//const t = (planeNormal.dot(planePoint).sub(planeNormal.dot(linePoint))).div(planeNormal.dot(lineDirection.normalized()));
const t = (planeNormal.dot(planePoint) - planeNormal.dot(linePoint)) / planeNormal.dot(lineDirection.normalized());
return linePoint.add(lineDirection.normalized().scale(t));
}
Vector.prototype.normalReflectingBetweenPoints = function(start, end) {
const mirror_pos = this;
let mirror_to_start = start.sub(mirror_pos).normalized();
let mirror_to_end = end.sub(mirror_pos).normalized();
let normal = mirror_to_start.add(mirror_to_end).normalized();
return normal;
}
Vector.prototype.angleBetweenInXZPlane = function(other) {
let a = this.normalized()
let b = other.normalized()
// atan2 return -PI to +PI
let result = Math.atan2(b.z,b.x) - Math.atan2(a.z,a.x);
// restrict value to be between -PI and +PI
//if (result < 0) result += Math.PI * 2;
if (result > +Math.PI) result -= Math.PI * 2;
if (result < -Math.PI) result += Math.PI * 2;
//if (result > Math.PI || result < -Math.PI) throw new Error(`${result} is not in range`)
return result;
}
Vector.prototype.angleBetweenInXYPlane = function(other) {
let a = this.normalized()
let b = other.normalized()
// atan2 return -PI to +PI
let result = Math.atan2(b.y,b.x) - Math.atan2(a.y,a.x);
// restrict value to be between -PI and +PI
//if (result < 0) result += Math.PI * 2;
if (result > +Math.PI) result -= Math.PI * 2;
if (result < -Math.PI) result += Math.PI * 2;
//if (result > Math.PI || result < -Math.PI) throw new Error(`${result} is not in range`)
return result;
}
Vector.prototype.angleBetween = function(other) {
// returns angle 0 to 180 (in radians) and the axis (that can then be used to figure out direction)
// the axis is perpendicular to the two input vectors
let angle = Math.acos(this.normalized().dot(other.normalized()));
let axis = this.normalized().cross(other.normalized()).normalized();
return {angle, axis};
}
Vector.prototype.angleTo = function(other) {
// returns a value of 0 when completely aligned to Math.PI when completely opposite
//angle = arccos[(xa * xb + ya * yb + za * zb) / (√(xa2 + ya2 + za2) * √(xb2 + yb2 + zb2))]
// from https://www.omnicalculator.com/math/angle-between-two-vectors
return Math.acos((this.x * other.x + this.y * other.y + this.z * other.z) / (Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z) * Math.sqrt(other.x * other.x + other.y * other.y + other.z * other.z)))
}
Vector.prototype.add = function(other) {
let result;
if(isVector(other)) {
result = add([this.x, this.y, this.z], [other.x, other.y, other.z]);
} else {
result = add([this.x, this.y, this.z], other);
}
return vector(result[0], result[1], result[2]);
}
Vector.prototype.clone = function() {
return vector(this.x, this.y, this.z);
}
Vector.prototype.cross = function(other) {
const result = cross([this.x, this.y, this.z], [other.x, other.y, other.z]);
return vector(result[0], result[1], result[2]);
}
Vector.prototype.dist = function(other) {
const result = dist([this.x, this.y, this.z], [other.x, other.y, other.z]);
return result
}
Vector.prototype.div = function(other) {
let result;
if(isVector(other)) {
result = div([this.x, this.y, this.z], [other.x, other.y, other.z]);
} else {
result = div([this.x, this.y, this.z], other);
}
return vector(result[0], result[1], result[2]);
}
Vector.prototype.dot = function(other) {
const result = dot([this.x, this.y, this.z], [other.x, other.y, other.z]);
return result
}
Vector.prototype.mag = function() {
const result = mag([this.x, this.y, this.z]);
return result
}
Vector.prototype.mult = function(other) {
let result;
if(isVector(other)) {
result = mult([this.x, this.y, this.z], [other.x, other.y, other.z]);
} else {
result = mult([this.x, this.y, this.z], other);
}
return vector(result[0], result[1], result[2]);
}
Vector.prototype.scale = function(other) {
return this.mult(other);
}
Vector.prototype.normalized = function(scalar) {
let result;
result = normalize([this.x, this.y, this.z], scalar);
return vector(result[0], result[1], result[2]);
}
Vector.prototype.sub = function(other) {
let result;
if(isVector(other)) {
result = sub([this.x, this.y, this.z], [other.x, other.y, other.z]);
} else {
result = sub([this.x, this.y, this.z], other);
}
return vector(result[0], result[1], result[2]);
}
Vector.prototype.withX = function(value) {
return vector(value, this.y, this.z);
}
Vector.prototype.withY = function(value) {
return vector(this.x, value, this.z);
}
Vector.prototype.withZ = function(value) {
return vector(this.x, this.y, value);
}
Vector.prototype.negated = function(other) {
return this.scale(-1.0);
}
Vector.prototype.rotatedAroundZ = function(angle, pivot) {
pivot = pivot || vector(0,0,0);
return vector(Math.cos(angle) * (this.x - pivot.x) - Math.sin(angle) * (this.y - pivot.y) + pivot.x,
Math.sin(angle) * (this.x - pivot.x) + Math.cos(angle) * (this.y - pivot.y) + pivot.y,
this.z)
}
Vector.prototype.rotatedAroundY = function(angle, pivot) {
pivot = pivot || vector(0,0,0);
return vector(Math.cos(angle) * (this.x - pivot.x) + Math.sin(angle) * (this.z - pivot.z) + pivot.x,
this.y,
Math.sin(angle) * -(this.x - pivot.x) + Math.cos(angle) * (this.z - pivot.z) + pivot.z)
}
Vector.prototype.rotatedAroundX = function(angle, pivot) {
pivot = pivot || vector(0,0,0);
return vector(this.x,
Math.cos(angle) * (this.y - pivot.y) - Math.sin(angle) * (this.z - pivot.z) + pivot.y,
Math.sin(angle) * (this.y - pivot.y) + Math.cos(angle) * (this.z - pivot.z) + pivot.z)
}
Vector.prototype.toFixed = function(precision) {
return {
x: this.x.toFixed(precision),
y: this.y.toFixed(precision),
z: this.z.toFixed(precision),
}
}
function isVector(thing) {
return thing.hasOwnProperty('x') && thing.hasOwnProperty('y') && thing.hasOwnProperty('z');
}
function vector(x,y,z) {
return new Vector(x,y,z);
}
Vector.prototype.array = function() {
return [this.x, this.y, this.z];
}
Vector.prototype.string = function(precision, scale) {
precision = precision || 3;
scale = scale || 1.0;
return `${(this.x * scale).toFixed(precision)}, ${(this.y * scale).toFixed(precision)}, ${(this.z * scale).toFixed(precision)}`;
}