UNPKG

sussy-util

Version:
108 lines (107 loc) 4.4 kB
import Vector2d from './Vector2d'; export default class Vector3d { readonly x: number; readonly y: number; readonly z: number; constructor(x?: number, y?: number, z?: number); /** * Creates a new Vector3d instance with the same x, y, and z values as the current vector. * @returns A new Vector3d instance. */ clone(): Vector3d; /** * 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: Vector3d): Vector3d; /** * 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: Vector3d): Vector3d; /** * 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: number): Vector3d; /** * 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: number): Vector3d; /** * Calculates the magnitude (length) of the vector. * @returns The magnitude of the vector. */ magnitude(): number; /** * 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(): Vector3d; /** * 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: Vector3d): number; /** * 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: Vector3d): Vector3d; /** * 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: Vector3d): number; /** * 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: Vector3d): boolean; /** * 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: Vector3d): boolean; /** * Returns a vector with the absolute values of the original vector's components. * @returns {Vector3d} - A new Vector2d object with absolute values. */ abs(): Vector3d; /** * 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: Vector3d): boolean; /** * Converts the vector to an array representation. * @returns An array containing the x, y, and z components of the vector. */ toArray(): number[]; /** * Returns a string representation of the vector in the format "(x, y, z)". * @returns A string representation of the vector. */ toString(): string; /** * 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: number[]): Vector3d; static of2d(other: Vector2d): Vector3d; }