/** * Class representing a 2D vector or point. * Implements a fluent, mutating API to reduce object allocations. */ export default class Vector2 { private _x; private _y; /** Optional callback fired when x or y is modified. */ onChange?: () => void; /** X coordinate. */ get x(): number; set x(val: number); /** Y coordinate. */ get y(): number; set y(val: number); /** * initialises a new instance of Vector2. * @param x Initial x coordinate. * @param y Initial y coordinate. */ constructor(x?: number, y?: number); /** * Sets the components of this vector. * @param x New x coordinate. * @param y New y coordinate. * @returns This vector for chaining. */ set(x: number, y: number): this; /** * Copies the components of another vector into this one. * @param other The vector to copy from. * @returns This vector for chaining. */ copy(other: Vector2): this; /** * Calculates the Euclidean distance between two vectors. * @param v1 First vector. * @param v2 Second vector. * @returns The distance between v1 and v2. */ static distance(v1: Vector2, v2: Vector2): number; /** * Calculates the dot product of two vectors. * @param v1 First vector. * @param v2 Second vector. * @returns The dot product of v1 and v2. */ static dot(v1: Vector2, v2: Vector2): number; /** * The length (magnitude) of the vector. */ get magnitude(): number; /** * A new normalized (unit length) version of this vector. * Does not mutate the original vector. */ get normalized(): Vector2; /** * Normalizes this vector in place. * @returns This vector for chaining. */ normalize(): this; /** * Adds another vector to this one (mutates). * @param other The vector to add. * @returns This vector for chaining. */ add(other: Vector2): this; /** * Subtracts another vector from this one (mutates). * @param other The vector to subtract. * @returns This vector for chaining. */ subtract(other: Vector2): this; /** * Multiplies this vector by a scalar (mutates). * @param scalar The value to multiply by. * @returns This vector for chaining. */ multiply(scalar: number): this; /** * Creates a new vector with the same x and y values as this one. * @returns A new Vector2 instance. */ clone(): Vector2; }