UNPKG

toosoon-utils

Version:
494 lines (493 loc) 15.1 kB
import type { Point2 } from '../../types'; import type { Vector } from './Vector'; type Vec2 = Vector2 | Point2; /** * Utility class for manipulating a 2D vectors * * @exports * @class Vector2 * @implements Vector */ export default class Vector2 implements Vector<Vec2> { readonly isVector2 = true; readonly type: string; /** * X-axis value of this vector */ x: number; /** * Y-axis value of this vector */ y: number; [Symbol.iterator](): Iterator<number>; /** * @param {number} [x=0] X-axis value * @param {number} [y=0] Y-axis value */ constructor(x?: number, y?: number); /** * Set this vector values * * @param {number} x X-axis value * @param {number} y Y-axis value * @returns {this} */ set(x: number, y: number): this; /** * Set a given scalar value to all values of this vector * * @param {number} scalar Value to set for all values * @returns {this} */ setScalar(scalar: number): this; /** * Set this vector X-axis value * * @param {number} x X-axis value to set * @returns {this} */ setX(x: number): this; /** * Set this vector Y-axis value * * @param {number} y Y-axis value to set * @returns {this} */ setY(y: number): this; /** * Set a given value of this vector * * @param {string|number} index `0` equals to `x`, `1` equals to `y` * @param {number} value Value to set * @returns {this} */ setValue(index: 'x' | 'y' | number, value: number): this; /** * Return a value from this vector * * @param {string|number} index `0` equals to `x`, `1` equals to `y` * @returns {number} */ getValue(index: 'x' | 'y' | number): number; /** * Add a given vector to this vector * * @param {Vector2|Point2} vector Vector to add * @returns {this} */ add([x, y]: Vec2): this; /** * Add a given scalar value to all values of this vector * * @param {number} scalar Scalar value to add * @returns {this} */ addScalar(scalar: number): this; /** * Subtract a given vector to this vector * * @param {Vector2|Point2} vector Vector to subtract * @returns {this} */ sub([x, y]: Vec2): this; /** * Subtract a given scalar value to all values of this vector * * @param {number} scalar Scalar value to subtract * @returns {this} */ subScalar(scalar: number): this; /** * Multiply a given vector to this vector * * @param {Vector2|Point2} vector Vector to multiply * @returns {this} */ multiply([x, y]: Vec2): this; /** * Multiply a given scalar value to all values of this vector * * @param {number} scalar Scalar value to multiply * @returns {this} */ multiplyScalar(scalar: number): this; /** * Divide a given vector to this vector * * @param {Vector2|Point2} vector Vector to divide * @returns {this} */ divide([x, y]: Vec2): this; /** * Divide a given scalar value to all values of this vector * * @param {number} scalar Scalar value to multiply * @returns {this} */ divideScalar(scalar: number): this; /** * Set this vector values to the min values compared to a given vector * * @param {Vector2|Point2} vector Vector to compare values with * @returns {this} */ min([x, y]: Vec2): this; /** * Set this vector values to the max values compared to a given vector * * @param {Vector2|Point2} vector Vector to compare values with * @returns {this} */ max([x, y]: Vec2): this; /** * Clamp this vector values to given boundaries * * @param {Vector2|Point2} min Minimum boundaries * @param {Vector2|Point2} max Maximum boundaries * @returns {this} */ clamp([minX, minY]: Vec2, [maxX, maxY]: Vec2): this; /** * Clamp this vector values to given scalar values * * @param {Vector2|Point2} min Minimum scalar boundary * @param {Vector2|Point2} max Maximum scalar boundary * @returns {this} */ clampScalar(min: number, max: number): this; /** * Round down to the nearest integer value this vector values * * @returns {this} */ floor(): this; /** * Round up to the nearest integer value this vector values * * @returns {this} */ ceil(): this; /** * Round to the nearest integer value this vector values * * @returns {this} */ round(): this; /** * Remove any fractional digits of this vector values * * @returns {this} */ trunc(): this; /** * Set this vector values to their negative values * * @returns {this} */ negate(): this; /** * Rotate this vector around a given center by the given angle * * @param {Vector2|Point2} center Vector around which to rotate * @param {number} angle Angle to rotate (in radians) * @returns {this} */ rotateAround(center: Vec2, angle: number): this; /** * Linearly interpolate this vector values towards a given vector values * * @param {number} t Normalized time value to interpolate * @param {Vector2|Point2} vector Vector to interpolate values towards * @returns {this} */ lerp(t: number, [x, y]: Vec2): this; /** * Convert this vector to a unit vector * * @returns {this} */ normalize(): this; /** * Transform this vector by a given matrix * * @param {DOMMatrix} matrix Matrix to apply * @returns {this} */ applyMatrix(matrix: DOMMatrix): this; /** * Set this vector values to the same direction but with a given length * * @param {number} length Length value * @returns {this} */ setLength(length: number): this; /** * Calculate the Euclidean length of this vector * * @returns {number} Computed Euclidean length */ length(): number; /** * Calculate the squared length of this vector * * @return {number} Computed squared length */ squaredLength(): number; /** * Calculate the Manhattan length of this vector * * @return {number} Computed Manhattan length */ manhattanLength(): number; /** * Check if this vector is equal with a given vector * * @param {Vector2|Point2} vector Vector to check * @returns {boolean} True if this vector is equal with the given vector, false otherwise */ equals(vector: Vec2): boolean; /** * Check if this vector is collinear with given vectors * * @param {Vector2|Point2} vector1 First vector to check * @param {Vector2|Point2} vector2 Second vector to check * @returns {boolean} True if this vector is collinear with the given vectors, false otherwise */ collinear(vector1: Vec2, vector2: Vec2): boolean; /** * Calculate the dot product of a given vector with this vector * * @param {Vector2|Point2} vector Vector to compute the dot product with * @returns {number} Computed dot product */ dot(vector: Vec2): number; /** * Calculate the cross product of a given vector with this vector * * @param {Vector2|Point2} vector Vector to compute the cross product with * @returns {number} Computed cross product */ cross(vector: Vec2): number; /** * Calculate the angle of this vector with respect to the positive X-axis * * @returns {number} Computed angle (in radians) */ angle(): number; /** * Calculate the angle between a given vector and this vector * * @param {Vector2|Point2} vector Vector to compute the angle with * @returns {number} Computed angle (in radians) */ angleTo(vector: Vec2): number; /** * Calculate the Euclidean distance from a given vector to this vector * * @param {Vector2|Point2} vector Vector to compute the distance to * @returns {number} Computed Euclidean distance */ distanceTo(vector: Vec2): number; /** * Calculate the squared distance from a given vector to this vector * * @param {Vector2|Point2} vector Vector to compute the squared distance to * @returns {number} Computed squared distance */ squaredDistanceTo(vector: Vec2): number; /** * Calculate the Manhattan distance from a given vector to this vector * * @param {Vector2|Point2} vector Vector to compute the Manhattan distance to * @returns {number} Computed Manhattan distance */ manhattanDistanceTo(vector: Vec2): number; /** * Return this vector values into an array * * @returns {Point2} */ toArray(): Point2; /** * Set this vector values from a given array * * @param {number[]} values Values to set * @returns {this} */ fromArray([x, y]: number[]): this; /** * Set this vector values from given circular coordinates * * @param {number} angle Angle (in radians) * @param {number} [radius] Radius of the circle * @returns {this} */ fromCircularCoords(angle: number, radius?: number): this; /** * Copy the values of a given vector to this vector * * @param {Vector2|Point2} vector Vector to copy values from * @returns {this} */ copy([x, y]: Vec2): this; /** * Create a new 2D vector with copied values from this vector * * @returns {Vector2} */ clone(): Vector2; /** * X-axis value of this vector */ set width(width: number); get width(): number; /** * Y-axis value of this vector */ set height(height: number); get height(): number; /** * Add two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {Point2} */ static add([x1, y1]: Vec2, [x2, y2]: Vec2): Point2; /** * Subtract two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {Point2} */ static sub([x1, y1]: Vec2, [x2, y2]: Vec2): Point2; /** * Multiply two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {Point2} */ static multiply([x1, y1]: Vec2, [x2, y2]: Vec2): Point2; /** * Divide two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {Point2} */ static divide([x1, y1]: Vec2, [x2, y2]: Vec2): Point2; /** * Rotate a vector around a given center by a given angle * * @param {Vector2|Point2} vector Vector to rotate * @param {Vector2|Point2} center Vector around which to rotate * @param {number} angle Angle to rotate (in radians) * @returns {Point2} */ static rotate([vx, vy]: Vec2, [cx, cy]: Vec2, angle: number): Point2; /** * Linearly interpolate a point between two vectors * * @param {number} t Normalized time value to interpolate * @param {Vector2|Point2} min Minimum boundaries * @param {Vector2|Point2} max Maximum boundaries * @returns {Point2} */ static lerp(t: number, [x1, y1]: Vec2, [x2, y2]: Vec2): Point2; /** * Check if two vectors are equal to each other * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {boolean} True if the given vectors are equal, false otherwise */ static equals([x1, y1]: Vec2, [x2, y2]: Vec2): boolean; /** * Check if three vectors are collinear (aligned on the same line) * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @param {Vector2|Point2} vector3 Third vector * @returns {boolean} True if the given vectors are collinear, false otherwise */ static collinear([x1, y1]: Vec2, [x2, y2]: Vec2, [x3, y3]: Vec2): boolean; /** * Calculate the dot product of two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {number} Computed dot product */ static dot([x1, y1]: Vec2, [x2, y2]: Vec2): number; /** * Calculate the cross product of two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {number} Computed cross product */ static cross([x1, y1]: Vec2, [x2, y2]: Vec2): number; /** * Calculate the angle of a given vector with respect to the positive X-axis * * @param {Vector2|Point2} vector Vector to compute angle from * @returns {number} Computed angle (in radians) */ static angle([x, y]: Vec2): number; /** * Calculate the Euclidean distance between two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {number} Computed Euclidean distance */ static distance(vector1: Vec2, vector2: Vec2): number; /** * Calculate the squared distance between two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @returns {number} Computed squared distance */ static squaredDistance([x1, y1]: Vec2, [x2, y2]: Vec2): number; /** * Calculate the Manhattan distance between two vectors * * @param {Vector2|Point2} vector1 First vector * @param {Vector2|Point2} vector2 Second vector * @return {number} Computed Manhattan distance */ static manhattanDistance([x1, y1]: Vec2, [x2, y2]: Vec2): number; /** * Calculate the Euclidean length of a vector * * @param {Vector2|Point2} vector Vector to compute Euclidean length from * @returns {number} Computed Euclidean length */ static length(vector: Vec2): number; /** * Calculate the squared length of a vector * * @param {Vector2|Point2} vector Vector to compute squared length from * @returns {number} Computed squared length */ static squaredLength([x, y]: Vec2): number; /** * Calculate the Manhattan length of a vector * * @param {Vector2|Point2} vector Vector to compute Manhattan length from * @return {number} Computed Manhattan length */ static manhattanLength([x, y]: Vec2): number; /** * Convert circular coordinates to a 2D point on the surface of a circle * * @param {number} angle Angle (in radians) * @param {number} [radius=1] Radius of the circle * @returns {Point2} */ static fromCircularCoords(angle: number, radius?: number): Point2; } export {};