UNPKG

vecti

Version:

A tiny TypeScript library for 2D vector math.

87 lines (86 loc) 2.94 kB
/** * An immutable 2D Vector that supports various operations. * @public */ export declare class Vector { readonly x: number; readonly y: number; /** * Create a vector with the given components. * @param x - The component of the x-axis. * @param y - The component of the y-axis. * @returns The vector. */ static of([x, y]: [number, number]): Vector; /** * Create a vector with the given components. * @param x - The component of the x-axis. * @param y - The component of the y-axis. * @returns The vector. */ constructor(x: number, y: number); /** * Add another vector to the vector. * @param val - The vector to be added. * @returns The resulting vector of the addition. */ add(val: Vector): Vector; /** * Subtract another vector from the vector. * @param val - The vector to be added. * @returns The resulting vector of the subtraction. */ subtract(val: Vector): Vector; /** * Multiply the vector by a scalar. * @param scalar - The scalar the vector will be multiplied by. * @returns The resulting vector of the multiplication. */ multiply(scalar: number): Vector; /** * Divide the vector by a scalar. * @param scalar - The scalar the vector will be divided by. * @returns The resulting vector of the division. */ divide(scalar: number): Vector; /** * Calculate the dot product of the vector and another vector. * @param other - The other vector used for calculating the dot product. * @returns The dot product. */ dot(other: Vector): number; /** * Calculate the cross product of the vector and another vector. The cross product of two vectors `a` and `b` is defined as `a.x * b.y - a.y * b.x`. * @param other - The other vector used for calculating the cross product. * @returns The cross product. */ cross(other: Vector): number; /** * Calculate the Hadamard product of the vector and another vector. * @param other - The other vector used for calculating the Hadamard product. * @returns The Hadamard product. */ hadamard(other: Vector): Vector; /** * Calculate the length of the vector using the L2 norm. * @returns The length. */ length(): number; /** * Normalize the vector using the L2 norm. * @returns The normalized vector. */ normalize(): Vector; /** * Rotate the vector by the given radians counterclockwise. * @param radians - The radians the vector will be rotated by. * @returns The rotated vector. */ rotateByRadians(radians: number): Vector; /** * Rotate the vector by the given degrees counterclockwise. * @param degrees - The degrees the vector will be rotated by. * @returns The rotated vector. */ rotateByDegrees(degrees: number): Vector; }