sussy-util
Version:
Util package made by me
168 lines (167 loc) • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Vector3d {
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Creates a new Vector3d instance with the same x, y, and z values as the current vector.
* @returns A new Vector3d instance.
*/
clone() {
return new Vector3d(this.x, this.y, this.z);
}
/**
* Adds another vector to the current vector and returns the result as a new vector.
* @param vector - The vector to be added.
* @returns The sum of the two vectors as a new Vector3d instance.
*/
add(vector) {
return new Vector3d(this.x + vector.x, this.y + vector.y, this.z + vector.z);
}
/**
* Subtracts another vector from the current vector and returns the result as a new vector.
* @param vector - The vector to be subtracted.
* @returns The difference between the two vectors as a new Vector3d instance.
*/
subtract(vector) {
return new Vector3d(this.x - vector.x, this.y - vector.y, this.z - vector.z);
}
/**
* Multiplies the vector by a scalar value and returns the result as a new vector.
* @param scalar - The scalar value to multiply the vector by.
* @returns The vector multiplied by the scalar value as a new Vector3d instance.
*/
multiply(scalar) {
return new Vector3d(this.x * scalar, this.y * scalar, this.z * scalar);
}
/**
* Divides the vector by a scalar value and returns the result as a new vector.
* @param scalar - The scalar value to divide the vector by.
* @returns The vector divided by the scalar value as a new Vector3d instance.
* @throws Throws an error if the scalar value is 0.
*/
divide(scalar) {
if (scalar === 0) {
throw new Error('Cannot divide by zero.');
}
return new Vector3d(this.x / scalar, this.y / scalar, this.z / scalar);
}
/**
* Calculates the magnitude (length) of the vector.
* @returns The magnitude of the vector.
*/
magnitude() {
return Math.sqrt(this.x ** 2 + this.y ** 2 + this.z ** 2);
}
/**
* Normalizes the vector to have a magnitude of 1 and returns the result as a new vector.
* @returns The normalized vector as a new Vector3d instance.
* @throws Throws an error if the vector is a zero vector (magnitude is 0).
*/
normalize() {
const magnitude = this.magnitude();
if (magnitude === 0) {
throw new Error('Cannot normalize a zero vector.');
}
return this.divide(magnitude);
}
/**
* Calculates the dot product of the current vector and another vector.
* @param vector - The other vector.
* @returns The dot product of the two vectors.
*/
dotProduct(vector) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
}
/**
* Calculates the cross product of the current vector and another vector.
* @param vector - The other vector.
* @returns The cross product of the two vectors as a new Vector3d instance.
*/
crossProduct(vector) {
const crossX = this.y * vector.z - this.z * vector.y;
const crossY = this.z * vector.x - this.x * vector.z;
const crossZ = this.x * vector.y - this.y * vector.x;
return new Vector3d(crossX, crossY, crossZ);
}
/**
* Calculates the angle in radians between the current vector and another vector.
* @param vector - The other vector.
* @returns The angle between the two vectors in radians.
*/
angleTo(vector) {
const thisMagnitude = this.magnitude();
const vectorMagnitude = vector.magnitude();
if (vectorMagnitude != 0 && thisMagnitude != 0 && this.normalize().equals(vector.normalize()))
return 0;
const dotProduct = this.dotProduct(vector);
const clampedDotProduct = Math.max(-1, Math.min(1, dotProduct));
return Math.acos(clampedDotProduct / (thisMagnitude * vectorMagnitude));
}
/**
* Determines if the current vector is parallel to another vector.
* @param vector - The other vector.
* @returns True if the vectors are parallel, false otherwise.
*/
isParallelTo(vector) {
const ratioX = this.x / vector.x;
const ratioY = this.y / vector.y;
const ratioZ = this.z / vector.z;
return Math.abs(ratioX - ratioY) < Number.EPSILON && Math.abs(ratioY - ratioZ) < Number.EPSILON;
}
/**
* Determines if the current vector is perpendicular (orthogonal) to another vector.
* @param vector - The other vector.
* @returns True if the vectors are perpendicular, false otherwise.
*/
isPerpendicularTo(vector) {
return this.dotProduct(vector) === 0;
}
/**
* Returns a vector with the absolute values of the original vector's components.
* @returns {Vector3d} - A new Vector2d object with absolute values.
*/
abs() {
return new Vector3d(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z));
}
/**
* Checks if the vector is equal to another vector.
* @param {Vector3d} vector - The vector to compare with.
* @returns {boolean} - True if the vectors are equal, false otherwise.
*/
equals(vector) {
return this.x === vector.x && this.y === vector.y && this.z === vector.z;
}
/**
* Converts the vector to an array representation.
* @returns An array containing the x, y, and z components of the vector.
*/
toArray() {
return [this.x, this.y, this.z];
}
/**
* Returns a string representation of the vector in the format "(x, y, z)".
* @returns A string representation of the vector.
*/
toString() {
return `(${this.x}, ${this.y}, ${this.z})`;
}
/**
* Creates a new Vector3d instance from an array representation.
* @param arr - An array containing the x, y, and z components of the vector.
* @returns A new Vector3d instance created from the array.
* @throws Throws an error if the array length is not 3.
*/
static ofArray(arr) {
if (arr.length !== 3)
throw new Error('Invalid array length. Array must contain three elements.');
return new Vector3d(arr[0], arr[1], arr[2]);
}
static of2d(other) {
return new Vector3d(other.x, other.y);
}
}
exports.default = Vector3d;