@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
95 lines • 3.02 kB
JavaScript
export class AABB2 {
constructor(left = 0, top = 0, width = 0, height = 0) {
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}
set(left, top, width, height) {
this.top = top;
this.left = left;
this.width = width;
this.height = height;
return this;
}
clone() {
return new AABB2(this.left, this.top, this.width, this.height);
}
copy(aabb) {
this.top = aabb.top;
this.left = aabb.left;
this.width = aabb.width;
this.height = aabb.height;
return this;
}
static from({ top, left, width, height, }, target) {
return target ? target.set(left, top, width, height) : new AABB2(left, top, width, height);
}
extend(other) {
if (other.left < this.left) {
this.width += this.left - other.left;
this.left = other.left;
}
if (other.right > this.right) {
this.width = other.right - this.left;
}
if (other.top < this.top) {
this.height += this.top - other.top;
this.top = other.top;
}
if (other.bottom > this.bottom) {
this.height = other.bottom - this.top;
}
return this;
}
get right() {
return this.left + this.width;
}
get bottom() {
return this.top + this.height;
}
get centerX() {
return this.left + this.width / 2;
}
set centerX(x) {
this.left += x - this.centerX;
}
get centerY() {
return this.top + this.height / 2;
}
set centerY(y) {
this.top += y - this.centerY;
}
is(left, top, width, height) {
return this.top === top && this.left === left && this.width === width && this.height === height;
}
isEqual(aabb) {
return this.top === aabb.top && this.left === aabb.left && this.width === aabb.width && this.height === aabb.height;
}
isInside(x, y) {
return this.left <= x && x < this.right && this.top <= y && y < this.bottom;
}
isInsideAABB(aabb) {
return (this.isInside(aabb.top, aabb.left) &&
this.left <= aabb.right &&
aabb.right <= this.right &&
this.top <= aabb.bottom &&
aabb.bottom <= this.bottom);
}
isIntersecting(aabb) {
return !(aabb.right <= this.left || aabb.left >= this.right || aabb.bottom <= this.top || aabb.top >= this.bottom);
}
isNorthWest(x, y) {
return (this.right <= x || this.left < x) && (this.top < y || this.bottom <= y);
}
isNorthEast(x, y) {
return (this.right > x || this.left >= x) && (this.top < y || this.bottom <= y);
}
isSouthEast(x, y) {
return (this.right > x || this.left >= x) && (this.top >= y || this.bottom > y);
}
isSouthWest(x, y) {
return (this.right <= x || this.left < x) && (this.top >= y || this.bottom > y);
}
}
//# sourceMappingURL=AABB2.js.map