malwoden
Version:
   
70 lines • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rect = void 0;
/** Represents a basic rectangle. */
var Rect = /** @class */ (function () {
/**
* Creates a rectangle. Will internally set v1 to the min
* coordinate corder, and v2 to the max corner.
*
* @param v1 - A vector representing one of the corners
* @param v2 - A vector representing the other corner
*/
function Rect(v1, v2) {
this.v1 = {
x: Math.min(v1.x, v2.x),
y: Math.min(v1.y, v2.y),
};
this.v2 = {
x: Math.max(v1.x, v2.x),
y: Math.max(v1.y, v2.y),
};
}
/**
* Creates a rect from width + height. The width x height of a 1x1 block
* would be 1, with v2 equal to v1.
* @param v1 Vector2 - The starting vector
* @param width number - The width. Min 1
* @param height number - The height - Min 1
* @returns Rect - A new rect with v2 computed from width/height
*/
Rect.FromWidthHeight = function (v1, width, height) {
if (width < 1) {
throw new Error("Rect width must be at least 1.");
}
if (height < 1) {
throw new Error("Rect height must be at least 1.");
}
var v2 = { x: v1.x + width - 1, y: v1.y + height - 1 };
return new Rect(v1, v2);
};
Rect.prototype.width = function () {
return Math.abs(this.v2.x - this.v1.x) + 1;
};
Rect.prototype.height = function () {
return Math.abs(this.v2.y - this.v1.y) + 1;
};
Rect.prototype.center = function () {
return {
x: Math.round((this.v1.x + this.v2.x - 1) / 2),
y: Math.round((this.v1.y + this.v2.y - 1) / 2),
};
};
Rect.prototype.intersects = function (rect) {
if (this.v1.x > rect.v2.x || rect.v1.x > this.v2.x)
return false;
if (this.v1.y > rect.v2.y || rect.v1.y > this.v2.y)
return false;
return true;
};
Rect.prototype.contains = function (point) {
if (this.v1.x > point.x || this.v2.x < point.x)
return false;
if (this.v1.y > point.y || this.v2.y < point.y)
return false;
return true;
};
return Rect;
}());
exports.Rect = Rect;
//# sourceMappingURL=rect.js.map