import { Point } from "./point";
import { Vector } from "./vector";
import { RNG } from "./rng";


export class AABB{
  source: Point
  size: Vector

  constructor(x:number, y:number, width:number, height:number){
    this.source = new Point(x, y)
    this.size = new Vector(width, height)
  }

  ///get corners clockwise starting at the upper left corner
  corners(): Point[]{
    return [
      this.source,
      new Point(this.source.x + this.size.x, this.source.y),
      this.source.add(this.size),
      new Point(this.source.x + this.size.x, this.source.y),
    ]
  }

  center(): Point{
    return this.source.add(this.size.scale(0.5))
  }

  containsPoint(other: Point): boolean{
    return other.gt(this.min()) && other.lt(this.max())
  }

  containsAABB(other: AABB): boolean{
    return this.min().lt(other.min()) && this.max().gt(other.max())
  }

  overlapsAABB(other: AABB): boolean{
    return this.min().lt(other.max()) && this.max().gt(other.min())
  }

  min(): Point{
    return this.source
  }

  area(): number{
    return this.size.x * this.size.y
  }

  max(): Point{
    return this.source.add(this.size)
  }

  randomPoint(generator?: RNG): Point{
    if (generator)
      //allow custom rng for deterministic values
      return this.source.add(this.size.scale2(generator.random(), generator.random()))
    else
      return this.source.add(this.size.scale2(Math.random(), Math.random()))
  }
}