UNPKG

sussy-util

Version:
154 lines (153 loc) 5.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Tuple_1 = __importDefault(require("./Tuple")); class Vector2d { constructor(x = 0, y = 0) { this.x = x; this.y = y; } /** * Creates a copy of the vector. * @returns {Vector2d} - A new Vector2d object with the same x and y coordinates. */ clone() { return new Vector2d(this.x, this.y); } /** * Adds another vector to the current vector. * @param {Vector2d} vector - The vector to be added. * @returns {Vector2d} - A new Vector2d object representing the sum of the two vectors. */ add(vector) { return new Vector2d(this.x + vector.x, this.y + vector.y); } /** * Subtracts another vector from the current vector. * @param {Vector2d} vector - The vector to be subtracted. * @returns {Vector2d} - A new Vector2d object representing the difference between the two vectors. */ subtract(vector) { return new Vector2d(this.x - vector.x, this.y - vector.y); } /** * Multiplies the vector by a scalar value. * @param {number} scalar - The scalar value to multiply the vector by. * @returns {Vector2d} - A new Vector2d object representing the scaled vector. */ multiply(scalar) { return new Vector2d(this.x * scalar, this.y * scalar); } /** * Divides the vector by a scalar value. * @param {number} scalar - The scalar value to divide the vector by. * @returns {Vector2d} - A new Vector2d object representing the divided vector. * @throws {Error} - Throws an error if the scalar is zero. */ divide(scalar) { if (scalar === 0) { throw new Error('Cannot divide by zero.'); } return new Vector2d(this.x / scalar, this.y / scalar); } /** * Calculates the magnitude (length) of the vector. * @returns {number} - The magnitude of the vector. */ magnitude() { return Math.sqrt(this.x * this.x + this.y * this.y); } /** * Returns a normalized version of the vector. * @returns {Vector2d} - A new Vector2d object representing the normalized vector. * @throws {Error} - Throws an error if the vector is a zero vector. */ normalize() { const magnitude = this.magnitude(); if (magnitude !== 0) { return new Vector2d(this.x / magnitude, this.y / magnitude); } else { throw new Error('Cannot normalize a zero vector.'); } } /** * Calculates the dot product of the vector with another vector. * @param {Vector2d} vector - The vector to calculate the dot product with. * @returns {number} - The dot product of the two vectors. */ dotProduct(vector) { return this.x * vector.x + this.y * vector.y; } /** * Calculates the distance between two vectors. * @param {Vector2d} vector - The vector to calculate the distance to. * @returns {number} - The distance between the two vectors. */ distanceTo(vector) { const dx = vector.x - this.x; const dy = vector.y - this.y; return Math.sqrt(dx * dx + dy * dy); } /** * Calculates the angle between two vectors in radians. * @param {Vector2d} vector - The vector to calculate the angle to. * @returns {number} - The angle between the two vectors in radians. */ angleTo(vector) { const magProduct = this.magnitude() * vector.magnitude(); if (magProduct != 0 && this.normalize().equals(vector.normalize())) return 0; const dot = this.dotProduct(vector); return Math.acos(dot / magProduct); } /** * Returns a vector with the absolute values of the original vector's components. * @returns {Vector2d} - A new Vector2d object with absolute values. */ abs() { return new Vector2d(Math.abs(this.x), Math.abs(this.y)); } /** * Checks if the vector is equal to another vector. * @param {Vector2d} 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; } toString() { return `(${this.x}, ${this.y})`; } /** * 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]; } /** * Converts the vector to an array representation. * @returns An array containing the x, y, and z components of the vector. */ toTuple() { return new Tuple_1.default(this.x, this.y); } /** * Creates a new Vector2d instance from an array representation. * @param arr - An array containing the x and y components of the vector. * @returns A new Vector2d instance created from the array. * @throws Throws an error if the array length is not 2. */ static ofArray(arr) { if (arr.length !== 2) throw new Error('Invalid array length. Array must contain three elements.'); return new Vector2d(arr[0], arr[1]); } static ofTuple(tuple) { return new Vector2d(tuple.first, tuple.second); } } exports.default = Vector2d;