export class Vector { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y } static fromAngle(angle: number) { return new Vector(Math.cos(angle), Math.sin(angle)) } static sub(v1: Vector, v2: Vector) { return new Vector(v1.x - v2.x, v1.y - v2.y) } static div(v1: Vector, divider: number): Vector { return new Vector(v1.x / divider, v1.y / divider) } add(other: Vector): Vector { this.x += other.x; this.y += other.y; return this } mult(scalar: number): Vector { this.x *= scalar; this.y *= scalar; return this } addMag(length: number): Vector { this.setMag(this.mag() + length); return this } setMag(length: number): Vector { let mag = this.mag(); this.x /= mag; this.y /= mag; this.x *= length; this.y *= length; return this } rotate(rad: number): Vector { let r = this.rotated(rad); this.x = r.x; this.y = r.y; return this } copy(): Vector { return new Vector(this.x, this.y) } heading(): number { let r = this.rotated(Math.PI / -2); return Math.atan2(r.x, -r.y) } mag() { return Math.sqrt(this.x * this.x + this.y * this.y) } serialized(): Serialized.Vector { return { x: this.x, y: this.y }; } private rotated(rad: number): Vector { let x = Math.cos(rad) * this.x - Math.sin(rad) * this.y, y = Math.sin(rad) * this.x + Math.cos(rad) * this.y; return new Vector(x, y) } } export let p = { createVector: (x: number, y: number): Vector => { return new Vector(x, y) }, dist: (x1: number, y1: number, x2: number, y2: number): number => { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) } };