


/**
 * 二维点
 */
export class Point {
  constructor(
    public readonly x: number,
    public readonly y: number
  ) { }
}



export class Rect {

  constructor(
    public readonly x: number,
    public readonly y: number,
    public readonly width: number,
    public readonly height: number
  ) { }

  get center(): Point {
    return new Point(this.x + this.width / 2, this.y + this.height / 2);
  }


  public static fromCenter(center: Point, width: number, height: number) {
    return new Rect(center.x - width / 2, center.y - height / 2, width, height);
  }


  toString(): string {
    return `Rect(${this.x}, ${this.y},${this.width}, ${this.height})`;
  }


  vertex(): Array<number> {
    var x1 = this.x;
    var x2 = this.x + this.width;
    var y1 = this.y;
    var y2 = this.y + this.height;

    return [
      x1, y1,
      x2, y1,
      x1, y2,
      x1, y2,
      x2, y1,
      x2, y2,
    ]
  }

  coverRadius(x: number, y: number): number {
    var mx, my;
    if (x > (this.x + this.width / 2)) mx = this.x
    else mx = this.x + this.width
  
    if (y > (this.y + this.height / 2)) my = this.y
    else my = this.y + this.height
  
    var dx = x - mx
    var dy = y - my
    return Math.sqrt(dx * dx + dy * dy)
  }



  centerFitTo(rect : Rect) : Rect {
    const scale = Math.min(rect.width /this.width, rect.height / this.height)
    const width = scale * this.width
    const height = scale * this.height
    const dx =  rect.width / 2 - width / 2
    const dy = rect.height / 2 - height /2
    return new Rect(rect.x + dx, rect.y + dy, width, height)
  }


}



export function coverRadius(rect: Rect, x: number, y: number) {
  var mx, my;
  if (x > (rect.x + rect.width / 2)) mx = rect.x
  else mx = rect.x + rect.width

  if (y > (rect.y + rect.height / 2)) my = rect.y
  else my = rect.y + rect.height

  var dx = x - mx
  var dy = y - my
  return Math.sqrt(dx * dx + dy * dy)
}



export function rectangle(x: number, y: number, w: number, h: number): Array<number> {
  return new Rect(x, y, w, h).vertex()
}




export function distance(x1: number, y1: number, x2: number, y2: number): number {
  let dx = x1 - x2
  let dy = y1 - y2
  return Math.sqrt(dx * dx + dy * dy)
}





export function fillRectangle(array: Float32Array, offset: number, x: number, y: number, width: number, height: number) {

  array[offset] = x
  array[offset + 1] = y

  array[offset + 2] = x + width
  array[offset + 3] = y

  array[offset + 4] = x
  array[offset + 5] = y + height

  array[offset + 6] = x
  array[offset + 7] = y + height

  array[offset + 8] = x + width
  array[offset + 9] = y

  array[offset + 10] = x + width
  array[offset + 11] = y + height
}




export function rectangleArray(x: number, y: number, width: number, height: number): Float32Array {
  const array = new Float32Array(12)
  const offset = 0

  array[offset] = x
  array[offset + 1] = y

  array[offset + 2] = x + width
  array[offset + 3] = y

  array[offset + 4] = x
  array[offset + 5] = y + height

  array[offset + 6] = x
  array[offset + 7] = y + height

  array[offset + 8] = x + width
  array[offset + 9] = y

  array[offset + 10] = x + width
  array[offset + 11] = y + height

  return array
}