sussy-util
Version:
Util package made by me
120 lines (119 loc) • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 dot = this.dotProduct(vector);
const magProduct = this.magnitude() * vector.magnitude();
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})`;
}
}
exports.default = Vector2d;