class Vector2 { public x: number; public y: number; constructor(x?: number, y?: number); constructor(v?: Vector2); constructor(x: Vector2 | number = 0, y: number = 0) { if (typeof x === 'object' && x !== null) { this.x = x.x; this.y = x.y; } else { this.x = x; this.y = y; } } add(s: number): Vector2; add(v: Vector2): Vector2; add(arg: number | Vector2): Vector2 { if (typeof arg === 'number') { return new Vector2(this.x + arg, this.y + arg); } else { return new Vector2(this.x + arg.x, this.y + arg.y); } } sub(s: number): Vector2; sub(v: Vector2): Vector2; sub(arg: number | Vector2): Vector2 { if (typeof arg === 'number') { return new Vector2(this.x - arg, this.y - arg); } else { return new Vector2(this.x - arg.x, this.y - arg.y); } } mul(s: number): Vector2; mul(v: Vector2): Vector2; mul(arg: number | Vector2): Vector2 { if (typeof arg === 'number') { return new Vector2(this.x * arg, this.y * arg); } else { return new Vector2(this.x * arg.x, this.y * arg.y); } } div(s: number): Vector2; div(v: Vector2): Vector2; div(arg: number | Vector2): Vector2 { if (typeof arg === 'number') { return new Vector2(this.x / arg, this.y / arg); } else { return new Vector2(this.x / arg.x, this.y / arg.y); } } equals(v: Vector2, epsilon: number = 1e-10): boolean { return Math.abs(this.x - v.x) < epsilon && Math.abs(this.y - v.y) < epsilon; } get sqrLength(): number { return this.x * this.x + this.y * this.y; } get length(): number { return Math.sqrt(this.sqrLength); } get normalized(): Vector2 { const len = this.length; return len === 0 ? new Vector2(0, 0) : new Vector2(this.x / len, this.y / len); } get angle(): number { return Math.atan2(this.y, this.x); } addEq(s: number): this; addEq(v: Vector2): this; addEq(arg: number | Vector2): this { if (typeof arg === 'number') { this.x += arg; this.y += arg; } else { this.x += arg.x; this.y += arg.y; } return this; } subEq(s: number): this; subEq(v: Vector2): this; subEq(arg: number | Vector2): this { if (typeof arg === 'number') { this.x -= arg; this.y -= arg; } else { this.x -= arg.x; this.y -= arg.y; } return this; } mulEq(s: number): this; mulEq(v: Vector2): this; mulEq(arg: number | Vector2): this { if (typeof arg === 'number') { this.x *= arg; this.y *= arg; } else { this.x *= arg.x; this.y *= arg.y; } return this; } divEq(s: number): this; divEq(v: Vector2): this; divEq(arg: number | Vector2): this { if (typeof arg === 'number') { this.x /= arg; this.y /= arg; } else { this.x /= arg.x; this.y /= arg.y; } return this; } cross(v: Vector2): number { return this.x * v.y - this.y * v.x; } dot(v: Vector2): number { return this.x * v.x + this.y * v.y; } set(x: number, y: number): this { this.x = x; this.y = y; return this; } normalize(): this { const len = this.length; if (len !== 0) { this.x /= len; this.y /= len; } return this; } get perpendicular(): Vector2 { return new Vector2(-this.y, this.x); } clone(): Vector2 { return new Vector2(this.x, this.y); } toString(): string { return `Vector2(${this.x}, ${this.y})`; } toArray(): [number, number] { return [this.x, this.y]; } [Symbol.iterator](): Iterator { let index = 0; const components = [this.x, this.y]; return { next: (): IteratorResult => { if (index < components.length) { return { value: components[index++], done: false }; } else { return { value: undefined as any, done: true }; } } }; } /** * Checks if this.xy below threshold, if so - set to 0 (each param individually) * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1) * @returns {this} new Vector2 with values of this or 0 * @example * // sets this.x or this.y to 0 if less then 0.0000001 * vecFoo.deadZone(1e-6); */ deadZone(threshold: number = 1e-4): Vector2 { return new Vector2( Math.abs(this.x) < threshold ? 0 : this.x, Math.abs(this.y) < threshold ? 0 : this.y ); } /** * Checks if *this*xy below threshold, if so - set to 0 (each param individually) * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1) * @returns {Vector2} this - i.e. the vector that calls the method. * @example * // sets this.x or this.y to 0 if less then 0.0000001 * vecFoo.deadZone(1e-6); */ deadZoneEq(threshold: number = 1e-4): this { if (Math.abs(this.x) < threshold) this.x = 0; if (Math.abs(this.y) < threshold) this.y = 0; return this; } static fromAngle(angle: number, magnitude: number = 1): Vector2 { return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude); } static dot(a: Vector2, b: Vector2): number { return a.x * b.x + a.y * b.y; } static cross(a: Vector2, b: Vector2): number { return a.x * b.y - a.y * b.x; } static distance(a: Vector2, b: Vector2): number { const dx = a.x - b.x; const dy = a.y - b.y; return Math.sqrt(dx * dx + dy * dy); } static distanceSqr(a: Vector2, b: Vector2): number { const dx = a.x - b.x; const dy = a.y - b.y; return dx * dx + dy * dy; } static lerp(a: Vector2, b: Vector2, t: number): Vector2 { return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); } static clamp(v: Vector2, min: Vector2, max: Vector2): Vector2 { return new Vector2( Math.min(Math.max(v.x, min.x), max.x), Math.min(Math.max(v.y, min.y), max.y) ); } static angleBetween(a: Vector2, b: Vector2): number { const dot = Vector2.dot(a, b); const mag = a.length * b.length; if (mag === 0) return 0; const cos = dot / mag; return Math.acos(Math.min(1, Math.max(-1, cos))); } static get zero(): Vector2 { return new Vector2(0, 0); } static get one(): Vector2 { return new Vector2(1, 1); } static get up(): Vector2 { return new Vector2(0, 1); } static get down(): Vector2 { return new Vector2(0, -1); } static get left(): Vector2 { return new Vector2(-1, 0); } static get right(): Vector2 { return new Vector2(1, 0); } } export default Vector2;