51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
let rectBound = function (xmin, ymin, xmax, ymax) {
|
|
if (xmin && ymin && xmax && ymax) {
|
|
this.xmin = xmin; this.xmax = xmax; this.ymin = ymin; this.ymax = ymax;
|
|
}
|
|
else this.init();
|
|
}
|
|
|
|
rectBound.prototype.init = function () {
|
|
this.xmin = this.ymin = Number.MAX_SAFE_INTEGER + 1;
|
|
this.xmax = this.ymax = Number.MIN_SAFE_INTEGER + 1
|
|
}
|
|
|
|
rectBound.prototype.updateP = function (v) {
|
|
if (v.x < this.xmin)
|
|
this.xmin = v.x;
|
|
if (v.y < this.ymin)
|
|
this.ymin = v.y;
|
|
if (v.x > this.xmax)
|
|
this.xmax = v.x;
|
|
if (v.y > this.ymax)
|
|
this.ymax = v.y;
|
|
}
|
|
|
|
rectBound.prototype.updateR = function (rect) {
|
|
if (this.xmin > rect.xmin)
|
|
this.xmin = rect.xmin;
|
|
if (this.ymin > rect.ymin)
|
|
this.ymin = rect.ymin;
|
|
if (this.xmax < rect.xmax)
|
|
this.xmax = rect.xmax;
|
|
if (this.ymax < rect.ymax)
|
|
this.ymax = rect.ymax;
|
|
}
|
|
|
|
rectBound.prototype.width = function () {
|
|
return Math.abs(this.xmax - this.xmin);
|
|
}
|
|
|
|
rectBound.prototype.height = function () {
|
|
return Math.abs(this.ymax - this.ymin);
|
|
}
|
|
|
|
rectBound.prototype.center = function () {
|
|
return { x: (this.xmin + (this.width() / 2)), y: this.ymin + (this.height() / 2) };
|
|
}
|
|
|
|
rectBound.prototype.contains = function (p) {
|
|
return (this.xmin <= p.x && p.x <= this.xmax && this.ymin <= p.y && p.y <= this.ymax);
|
|
}
|
|
|
|
module.exports = { Bound: rectBound } |