export class Rect {
    x;
    y;
    width;
    height;
    constructor(props) {
        this.x = props?.x ?? 0;
        this.y = props?.y ?? 0;
        this.width = props?.width ?? 0;
        this.height = props?.height ?? 0;
    }
    /**
     * Determines whether or not this rectangle wholly encloses another rectangle or point.
     **/
    contains(rect) {
        // points don't have width or height
        var otherWidth = rect.width || 0;
        var otherHeight = rect.height || 0;
        return (this.x <= rect.x &&
            this.y <= rect.y &&
            this.x + this.width >= rect.x + otherWidth &&
            this.y + this.height >= rect.y + otherHeight);
    }
    /**
     * Determines whether or not the rectangle intersects with another.
     **/
    overlaps(rect) {
        var thisRight = this.x + this.width;
        var thisBottom = this.y + this.height;
        var rectRight = rect.x + rect.width;
        var rectBottom = rect.y + rect.height;
        // http://stackoverflow.com/a/306332
        return this.x < rectRight && thisRight > rect.x && this.y < rectBottom && thisBottom > rect.y;
    }
    getMaximalFreeRects(rect) {
        // if no intersection, return false
        if (!this.overlaps(rect)) {
            return false;
        }
        var freeRects = [];
        var freeRect;
        var thisRight = this.x + this.width;
        var thisBottom = this.y + this.height;
        var rectRight = rect.x + rect.width;
        var rectBottom = rect.y + rect.height;
        // top
        if (this.y < rect.y) {
            freeRect = new Rect({
                x: this.x,
                y: this.y,
                width: this.width,
                height: rect.y - this.y,
            });
            freeRects.push(freeRect);
        }
        // right
        if (thisRight > rectRight) {
            freeRect = new Rect({
                x: rectRight,
                y: this.y,
                width: thisRight - rectRight,
                height: this.height,
            });
            freeRects.push(freeRect);
        }
        // bottom
        if (thisBottom > rectBottom) {
            freeRect = new Rect({
                x: this.x,
                y: rectBottom,
                width: this.width,
                height: thisBottom - rectBottom,
            });
            freeRects.push(freeRect);
        }
        // left
        if (this.x < rect.x) {
            freeRect = new Rect({
                x: this.x,
                y: this.y,
                width: rect.x - this.x,
                height: this.height,
            });
            freeRects.push(freeRect);
        }
        return freeRects;
    }
    canFit(rect) {
        return this.width >= rect.width && this.height >= rect.height;
    }
}
//# sourceMappingURL=Rect.js.map