import { Range } from "./Range"; import { Vector2 } from "./Vector2"; export class Box2 { min:Vector2; max:Vector2; static create( min:Vector2, max:Vector2 ):Box2 { let box = new Box2(); box.min = min; box.max = max; return box; } static polar( value:number ):Box2 { return this.create( new Vector2( -value, -value ), new Vector2( value, value ) ); } get size():Vector2 { return this.max.clone().sub( this.min ); } get center():Vector2 { return this.max.clone().add( this.min ).multiply( 0.5 ); } get rangeX():Range { return new Range( this.min.x, this.max.x ); } get rangeY():Range { return new Range( this.min.y, this.max.y ); } intersectsBox( box:Box2 ) { return this.rangeX.overlaps( box.rangeX ) && this.rangeY.overlaps( box.rangeY ); } translate( offset:Vector2 ) { this.min.add( offset ); this.max.add( offset ); } static fromClientRect( clientRect:ClientRect|DOMRect ) { let min = new Vector2( clientRect.left, clientRect.top ); let max = new Vector2( clientRect.right, clientRect.bottom ); return Box2.create( min, max ); } static toDomRect( box:Box2 ) { let size = box.size; let domRect = new DOMRect( box.min.x, box.min.y, size.x, size.y ); return domRect; } static updatefromClientRect( box:Box2, clientRect:ClientRect|DOMRect ) { box.min.set( clientRect.left, clientRect.top ); box.max.set( clientRect.right, clientRect.bottom ); } }