import { IPoint } from "pixi.js"

// my own point I can extend however I want to, compatible with pixijs points
export class Vector implements IPoint {
  x: number
  y: number

  constructor(x: number, y?: number) {
    if (y === undefined) y = x
    this.x = x
    this.y = y
  }

  normalize(): Vector {
    let len = this.length()
    if (len < 0.001) {
      return new Vector(0, 0)
    } else {
      return this.scale(1 / len)
    }
  }

  length(): number {
    return Math.sqrt(this.x * this.x + this.y * this.y)
  }

  scale(scalar: number): Vector {
    return new Vector(this.x * scalar, this.y * scalar)
  }

  scale2(x: number, y: number): Vector {
    return new Vector(this.x * x, this.y * y)
  }

  //IPoint interface functions
  set(x?: number, y?: number): void {
    if (x !== undefined) this.x = x
    if (y !== undefined) this.y = y
  }
  copyFrom(p: IPoint): this {
    this.set(p.x, p.y)
    return this
  }
  copyTo(p: IPoint): IPoint {
    p.set(this.x, this.y)
    return p
  }
  equals(p: IPoint): boolean {
    return this.x === p.x && this.y === p.y
  }
}
