import { Point } from '@pixi/math' ; public export class AxisAlignedBounds { public currentID : number ; public dirtyID : number ; protected _x : number ; protected _y : number ; protected _width : number ; protected _height : number ; protected _hull : [ Point , Point , Point , Point ] ; param x param y param width param height constructor ( x = 0 , y = 0 , width = 0 , height = 0 ) { this . _x = Number ( x ) ; this . _y = Number ( y ) ; this . _width = Number ( width ) ; this . _height = Number ( height ) ; this . _hull = [ new Point ( ) , new Point ( ) , new Point ( ) , new Point ( ) ] ; this . currentID = - 1 ; this . dirtyID = 0 ; } get x ( ) : number { return this . _x ; } set x ( value ) { this . _x = value ; this . dirtyID ++ ; } get y ( ) : number { return this . _y ; } set y ( value ) { this . _y = value ; this . dirtyID ++ ; } get width ( ) : number { return this . _width ; } set width ( value ) { this . _width = value ; this . dirtyID ++ ; } get height ( ) : number { return this . _height ; } set height ( value ) { this . _height = value ; this . dirtyID ++ ; } get hull ( ) : Point [ ] { if ( this . isDirty ( ) ) this . update ( ) ; return this . _hull ; } get topLeft ( ) : Point { if ( this . isDirty ( ) ) this . update ( ) ; return this . _hull [ 0 ] ; } get topRight ( ) : Point { if ( this . isDirty ( ) ) this . update ( ) ; return this . _hull [ 1 ] ; } get bottomRight ( ) : Point { if ( this . isDirty ( ) ) this . update ( ) ; return this . _hull [ 2 ] ; } get bottomLeft ( ) : Point { if ( this . isDirty ( ) ) this . update ( ) ; return this . _hull [ 3 ] ; } protected isDirty ( ) { return this . currentID !== this . dirtyID ; } protected update ( ) { const [ topLeft , topRight , bottomRight , bottomLeft ] = this . _hull ; topLeft . set ( this . _x , this . _y ) ; topRight . set ( this . _x + this . _width , this . _y ) ; bottomRight . set ( this . _x + this . _width , this . _y + this . _height ) ; bottomLeft . set ( this . _x , this . _y + this . _height ) ; } member get left ( ) : number { return this . x ; } /**
     * returns the right edge of the rectangle
     */ get right ( ) : number { return this . x + this . width ; } /**
     * returns the top edge of the rectangle
     */ get top ( ) : number { return this . y ; } /**
     * returns the bottom edge of the rectangle
     */ get bottom ( ) : number { return this . y + this . height ; } return clone ( ) { return new AxisAlignedBounds ( this . x , this . y , this . width , this . height ) ; } param rectangle return copyFrom ( rectangle ) { this . x = rectangle . x ; this . y = rectangle . y ; this . width = rectangle . width ; this . height = rectangle . height ; return this ; } param rectangle return copyTo ( rectangle ) { rectangle . x = this . x ; rectangle . y = this . y ; rectangle . width = this . width ; rectangle . height = this . height ; return rectangle ; } param x param y return contains ( x , y ) { if ( this . width <= 0 || this . height <= 0 ) { return false ; } if ( x >= this . x && x < this . x + this . width ) { if ( y >= this . y && y < this . y + this . height ) { return true ; } } return false ; } param bounds equals ( bounds ) { if ( ! bounds ) return false ; return bounds . x === this . x && bounds . y === this . y && bounds . width === this . width && bounds . height === this . height ; } param paddingX param paddingY return pad ( paddingX = 0 , paddingY = paddingX ) { this . x -= paddingX ; this . y -= paddingY ; this . width += paddingX * 2 ; this . height += paddingY * 2 ; return this ; } param rectangle return fit ( rectangle ) { const x1 = Math . max ( this . x , rectangle . x ) ; const x2 = Math . min ( this . x + this . width , rectangle . x + rectangle . width ) ; const y1 = Math . max ( this . y , rectangle . y ) ; const y2 = Math . min ( this . y + this . height , rectangle . y + rectangle . height ) ; this . x = x1 ; this . width = Math . max ( x2 - x1 , 0 ) ; this . y = y1 ; this . height = Math . max ( y2 - y1 , 0 ) ; return this ; } param resolution param eps return ceil ( resolution = 1 , eps = 0.001 ) { const x2 = Math . ceil ( ( this . x + this . width - eps ) * resolution ) / resolution ; const y2 = Math . ceil ( ( this . y + this . height - eps ) * resolution ) / resolution ; this . x = Math . floor ( ( this . x + eps ) * resolution ) / resolution ; this . y = Math . floor ( ( this . y + eps ) * resolution ) / resolution ; this . width = x2 - this . x ; this . height = y2 - this . y ; return this ; } param rectangle return enlarge ( rectangle ) { const x1 = Math . min ( this . x , rectangle . x ) ; const x2 = Math . max ( this . x + this . width , rectangle . x + rectangle . width ) ; const y1 = Math . min ( this . y , rectangle . y ) ; const y2 = Math . max ( this . y + this . height , rectangle . y + rectangle . height ) ; this . x = x1 ; this . width = x2 - x1 ; this . y = y1 ; this . height = y2 - y1 ; return this ; } }