All files / ts/extensions extensionsBounds2.ts

100% Statements 55/55
100% Branches 28/28
100% Functions 15/15
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x                                         1x 1x 1x 1x 1x   1x 20x 20x 20x   1x 19x 19x 19x 19x   1x 1x   1x 1x   1x 1x   1x 5x   1x 4x   1x 7x   1x 7x   1x 8x   1x 8x   1x 6x   1x 23x 9x   14x   1x 8x 6x   2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x  
import { IBounds2, Bounds2 } from "../models/bounds2.js"
import { IVector2, Vector2 } from "../models/vector2.js";
 
declare module "../models/bounds2.js" {
    interface IBounds2 {
        getScale(bounds: IBounds2): number;
        getCenter(bounds: IBounds2): IVector2;
        getRight(): number;
        getBottom(): number;
        getTopRight(): IVector2;
        getBottomRight(): IVector2;
        getBottomLeft(): IVector2;
        intersectTop(bounds: IBounds2): boolean;
        intersectBottom(bounds: IBounds2): boolean;
        intersectLeft(bounds: IBounds2): boolean;
        intersectRight(bounds: IBounds2): boolean;
        intersectsWith(bounds: IBounds2): boolean;
        contains(bounds: IBounds2): boolean;
        combine(bounds: IBounds2): IBounds2;
    }
    interface Bounds2 extends IBounds2 { }
}
Bounds2.prototype.set = function (bounds: IBounds2): void {
    this.x = bounds.x;
    this.y = bounds.y;
    this.width = bounds.width;
    this.height = bounds.height;
}
Bounds2.prototype.getScale = function (bounds: IBounds2): number {
    const scaleX = this.width / (bounds.width);
    const scaleY = this.height / (bounds.height);
    return scaleX < scaleY ? scaleX : scaleY;
}
Bounds2.prototype.getCenter = function (bounds: IBounds2): IVector2 {
    const vector = new Vector2();
    vector.x = (bounds.x + bounds.width * 0.5) - this.width * 0.5;
    vector.y = (bounds.y + bounds.height * 0.5) - this.height * 0.5;
    return vector;
}
Bounds2.prototype.getRight = function (): number {
    return this.x + this.width;
}
Bounds2.prototype.getBottom = function (): number {
    return this.y + this.height;
}
Bounds2.prototype.getTopRight = function (): IVector2 {
    return new Vector2(this.x + this.width, this.y);
}
Bounds2.prototype.getBottomRight = function (): IVector2 {
    return new Vector2(this.x + this.width, this.y + this.height);
}
Bounds2.prototype.getBottomLeft = function (): IVector2 {
    return new Vector2(this.x, this.y + this.height);
}
Bounds2.prototype.intersectTop = function (bounds: IBounds2): boolean {
    return this.y <= bounds.y + bounds.height;
}
Bounds2.prototype.intersectBottom = function (bounds: IBounds2): boolean {
    return this.y + this.height >= bounds.y;
}
Bounds2.prototype.intersectLeft = function (bounds: IBounds2): boolean {
    return this.x <= bounds.x + bounds.width;
}
Bounds2.prototype.intersectRight = function (bounds: IBounds2): boolean {
    return this.x + this.width >= bounds.x;
}
Bounds2.prototype.intersectsWith = function (bounds: IBounds2): boolean {
    return this.intersectLeft(bounds) && this.intersectRight(bounds) && this.intersectTop(bounds) && this.intersectBottom(bounds);
}
Bounds2.prototype.contains = function (bounds: IBounds2): boolean {
    if (this.width === 0 || this.height === 0)
        return false;
 
    return this.x <= bounds.x && this.x + this.width >= bounds.x + bounds.width && this.y <= bounds.y && this.y + this.height >= bounds.y + bounds.height;
}
Bounds2.prototype.combine = function (bounds: IBounds2): IBounds2 {
    if (this.width === 0 || this.height === 0 || bounds.width === 0 || bounds.height === 0)
        return bounds;
 
    const aBottomLeft = this.getBottomLeft();
    const bBottomLeft = bounds.getBottomLeft();
    const aBottomRight = this.getBottomRight();
    const bBottomRight = bounds.getBottomRight();
    const bottomLeft = aBottomLeft.y > bBottomLeft.y ? aBottomLeft.y : bBottomLeft.y;
    const bottomRight = aBottomRight.x > bBottomRight.x ? aBottomRight.x : bBottomRight.x;
 
    const combined = new Bounds2();
    combined.x = this.x < bounds.x ? this.x : bounds.x;
    combined.y = this.y < bounds.y ? this.y : bounds.y;
    combined.width = bottomRight - combined.x;
    combined.height = bottomLeft - combined.y;
    return combined;
}