tsinsim
Version:
An InSim library for Node.js (JavaScript runtime environment) with TypeScript support.
93 lines (92 loc) • 2.6 kB
JavaScript
export class Vector3 {
x = 0;
y = 0;
z = 0;
constructor(...args) {
if (args.length === 3) {
if (typeof args[0] == 'number' && typeof args[1] == 'number' && typeof args[2] == 'number') {
this.x = args[0];
this.y = args[1];
this.z = args[2];
}
}
else if (args.length === 1 && Array.isArray(args[0])) {
this.x = args[0][0];
this.y = args[0][1];
this.z = args[0][2];
}
else if (args.length === 1 && !Array.isArray(args[0]) && typeof args[0] === 'object') {
this.x = args[0].x;
this.y = args[0].y;
this.z = args[0].z;
}
return this;
}
set(x, y, z) {
this.x = x;
if (typeof y === 'number') {
this.y = y;
}
if (typeof z === 'number') {
this.z = z;
}
return this;
}
copy(vector) {
this.x = vector.x;
this.y = vector.y;
this.z = vector.z;
return this;
}
clone(vector) {
return new Vector3(vector.x, vector.y, vector.z);
}
add(value) {
this.x += value;
this.y += value;
this.z += value;
return this;
}
addX(value) { this.x += value; return this; }
addY(value) { this.y += value; return this; }
addZ(value) { this.z += value; return this; }
sub(vector) {
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
return this;
}
subX(value) { this.x -= value; return this; }
subY(value) { this.y -= value; return this; }
subZ(value) { this.z -= value; return this; }
mul(value) {
this.x *= value;
this.y *= value;
this.z *= value;
return this;
}
mulX(value) { this.x *= value; return this; }
mulY(value) { this.y *= value; return this; }
mulZ(value) { this.z *= value; return this; }
div(value) {
this.x *= value;
this.y *= value;
this.z *= value;
return this;
}
divX(value) { this.x /= value; return this; }
divY(value) { this.y /= value; return this; }
divZ(value) { this.z /= value; return this; }
lengthSq() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
length() {
return Math.sqrt(this.lengthSq());
}
distanceTo(vector) {
return Math.sqrt(this.distanceToSquared(vector));
}
distanceToSquared(vector) {
return new Vector3(this).sub(vector).lengthSq();
}
}