UNPKG

@equinor/videx-vector2

Version:

Vector2 class library written in javascript.

403 lines (402 loc) 12.2 kB
import { VectorLike } from '@equinor/videx-linear-algebra'; /** * Vector2 class with x and y component. Can also be used as an array with two indices, i.e. using [0] and [1]. * @class * @alias Vector2 */ export default class Vector2 { /** * Declare that Vector2 will have numeric properties. */ [index: number]: number; /** * Does the vector mutate? */ isMutating: boolean; /** * Length variable to use vector as array. */ length: number; /** * Construct a new Vector2 with identical x and y components. * @param value Value of x and y component */ constructor(value: number); /** * Construct a new Vector2. * @param x Initial x component of vector * @param y Initial y component of vector */ constructor(x: number, y: number); /** * Construct a new Vector2 using component from another vector. * @param vector */ constructor(vector: VectorLike); /** * Construct a new Vector2 using an object with an x and y key. * @param vector */ constructor(vector: { x: number; y: number; }); /** * Construct a new Vector2. * @param nums A series of numbers */ constructor(...nums: number[]); /** * Alternative getter/setter for x component. */ get x(): number; set x(value: number); /** * Alternative getter/setter for y component. */ get y(): number; set y(value: number); /** * Magnitude of vector. */ get magnitude(): number; set magnitude(val: number); /** * Set mutable and return reference to self. * @returns Reference to self */ get mutable(): Vector2; /** * Set immutable and return reference to self. * @returns Reference to self */ get immutable(): Vector2; /** * [Mutation] Set both components of vector. * @param value Value of x and y component * @returns Reference to self */ set(value: number): Vector2; /** * [Mutation] Set both components of vector. * @param x New x component of vector * @param y New y component of vector * @returns Reference to self */ set(x: number, y: number): Vector2; /** * [Mutation] Set both components of vector. * @param vector Vector-like object with values on the format: [ x, y ] * @returns Reference to self */ set(vector: VectorLike): Vector2; /** * Add values of given vector to target vector. * @param x X component to add * @param y Y component to add * @returns Resulting vector */ add(x: number, y: number): Vector2; /** * Add values of given vector to target vector. * @param vector Vector to add * @returns Resulting vector */ add(vector: VectorLike): Vector2; /** * a + b * * Add two values together. * @param a Left operand * @param b Right operand * @returns Resulting vector * @static */ static add(a: VectorLike, b: VectorLike): Vector2; /** * Subtract from vector. * @param x X component to subtract * @param y Y component to subtract * @returns Resulting vector */ sub(x: number, y: number): Vector2; /** * Subtract from vector. * @param vector Vector to subtract * @returns Resulting vector */ sub(vector: VectorLike): Vector2; /** * a - b * * Subtract second vector from first vector. * @param a Left operand * @param b Right operand * @returns Resulting vector */ static sub(a: VectorLike, b: VectorLike): Vector2; /** * target - this * * Subtract this vector from given vector. * @param x X component to subtract from * @param y Y component to subtract from * @returns Resulting vector */ subFrom(x: number, y: number): Vector2; /** * target - this * * Subtract this vector from given vector. * @param vector Vector to to subtract from * @returns Resulting vector */ subFrom(vector: VectorLike): Vector2; /** * v / n * * Divide vector by a numeric value. * @param v Vector to divide * @param n Numeric value * @returns Resulting vector */ static divide(v: VectorLike, n: number): Vector2; /** * v * n * * Multiply vector by a numeric value. * @param v Vector to multiply * @param n Numeric value * @returns Resulting vector */ static multiply(v: VectorLike, n: number): Vector2; /** * Scale vector by a numeric value. * @param n Numeric value * @returns Resulting vector */ scale(n: number): Vector2; /** * Rescale the vector to given length. * @param n Numeric value * @returns Resulting vector */ rescale(n: number): Vector2; /** * Ensures that the magnitude of the vector does not * exceed a given length. * @param {Number} n Numeric value * @returns {Vector2} Resulting vector */ clampMagnitude(n: number): Vector2; /** * Rotate the vector by specified amount of radians. Positive * rotation is counter-clockwise. * @param rad Radians to rotate * @returns Resulting vector */ rotate(rad: number): Vector2; /** * Rotate the vector by specified amount of degrees. Positive * rotation is counter-clockwise. * @param rad Degrees to rotate * @returns Resulting vector */ rotateDeg(deg: number): Vector2; /** * Rotate the vector counter-clockwise by an amount of 90 degrees. Resulting * vector is perpendicular to the original. * @returns Resulting vector */ rotate90(): Vector2; /** * Rotate the vector counter-clockwise by an amount of 180 degrees. Resulting * vector is opposite of original. * @returns Resulting vector */ rotate180(): Vector2; /** * Rotate the vector counter-clockwise by an amount of 270 degrees. Resulting * vector is perpendicular to the original. * @returns Resulting vector */ rotate270(): Vector2; /** * [Mutation] Normalizes the vector. * @returns Reference to vector */ normalize(): Vector2; /** * Get normalized version of vector. * @returns Resulting vector */ normalized(): Vector2; /** * Get distance between two positions. * @param a First position * @param b Second position * @returns Distance between positions */ static distance(a: VectorLike, b: VectorLike): number; /** * Get dot product between two vectors. * @param a First vector * @param b Second vector * @return Dot product */ static dot(a: VectorLike, b: VectorLike): number; /** * Get cross product between two vectors. * @param a First vector * @param b Second vector * @return Cross product */ static cross(a: VectorLike, b: VectorLike): number; /** * Get angle (in radians) between vector and [1, 0]. * @param v Target vector * @return Angle in radians */ static angleRight(v: VectorLike): number; /** * Get angle (in degrees) between vector and [1, 0]. * @param v Target vector * @return Angle in degrees */ static angleRightDeg(v: VectorLike): number; /** * Get angle (in radians) between two vectors. * @param a First vector * @param b Second vector * @returns Angle in radians */ static angle(a: VectorLike, b: VectorLike): number; /** * Get angle (in degrees) between two vectors. * @param a First vector * @param b Second vector * @returns Angle in degrees */ static angleDeg(a: VectorLike, b: VectorLike): number; /** * Get signed angle (in radians) between two vectors. * @param a First vector * @param b Second vector * @returns Signed angle in radians */ static signedAngle(a: VectorLike, b: VectorLike): number; /** * Get signed angle (in degrees) between two vectors. * @param a First vector * @param b Second vector * @returns Signed angle in degrees */ static signedAngleDeg(a: VectorLike, b: VectorLike): number; /** * Interpolate between two positions with given value n. * @param a Position to interpolate from * @param b Position to interpolate to * @param t Value between 0 - 1 used for interpolation * @returns Interpolated position */ static lerp(a: VectorLike, b: VectorLike, t: number): Vector2; /** * Rotates a vector, v1, towards a second vector, v2, based on a factor, n. * @param a Vector to interpolate from * @param b Vector to interpolate to * @param t Value between 0 - 1 used for interpolation * @returns Interpolated vector */ static lerpRot(a: VectorLike, b: VectorLike, t: number): Vector2; /** * Creates a new vector with identical values. * Mutable state is not transferred. * @returns Clone of vector */ clone(): Vector2; /** * Returns true if a equals b. Epsilon defines allowed deviation for the x and y component. * @param a Vector to evaluate * @param b Vector to compare with * @param epsilon Accepted deviation (Default: 0) * @returns Are vectors equal? */ static equals(a: VectorLike, b: VectorLike, epsilon?: number): boolean; /** * Returns true if vector equals b. Epsilon defines allowed deviation for the x and y component. * @param vector Vector to compare with * @param epsilon Accepted deviation (Default: 0) * @returns Are vectors equal? */ equals(vector: VectorLike, epsilon?: number): boolean; /** * Returns true if x and y is zero, otherwise returns false. * @param a Vector to evaluate * @param epsilon Accepted deviation from 0.00 (Default: 0) * @returns Is target zero vector? */ static isZeroVector(vector: VectorLike, epsilon?: number): boolean; /** * Returns true if x and y is zero, otherwise returns false. * @param epsilon Accepted deviation from 0.00 (Default: 0) * @returns Is target zero vector? */ isZeroVector(epsilon?: number): boolean; /** * Create an array from the vector. * @returns Array on the format: [ x, y ] */ toArray(): [number, number]; /** * [Mutation] Modifies both the x and y-component of a vector using a given function. * @param modifier Function used to modify * @returns Reference to vector */ modify(modifier: (d: number) => number): Vector2; [Symbol.iterator](): { next: () => { value: number; done: boolean; }; }; /** * Vector2 with values: [0, 0]. * @memberof Vector2 */ static get zero(): Vector2; /** * Vector2 with values: [1, 1]. * @memberof Vector2 */ static get one(): Vector2; /** * Vector2 with values: [∞, ∞]. * @memberof Vector2 */ static get positiveInfinity(): Vector2; /** * Vector2 with values: [-∞, -∞]. * @memberof Vector2 */ static get negativeInfinity(): Vector2; /** * Vector2 with values: [0, 1]. * @memberof Vector2 */ static get up(): Vector2; /** * Vector2 with values: [1, 0]. * @memberof Vector2 */ static get right(): Vector2; /** * Vector2 with values: [0, -1]. * @memberof Vector2 */ static get down(): Vector2; /** * Vector2 with values: [-1, 0]. * @memberof Vector2 */ static get left(): Vector2; }