UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

91 lines (90 loc) • 2.78 kB
import { Point } from "./Point.js"; //#region src/geometry/Bounds.js var Bounds = class Bounds { constructor(a, b) { if (!a) return; if (a instanceof Bounds) { if (a.isValid()) { this.min = new Point(a.min); this.max = new Point(a.max); } return; } const points = b ? [a, b] : a; for (const point of points) this.extend(point); } extend(obj) { let min2, max2; if (!obj) return this; if (obj instanceof Point || typeof obj[0] === "number" || "x" in obj) min2 = max2 = new Point(obj); else { obj = new Bounds(obj); min2 = obj.min; max2 = obj.max; if (!min2 || !max2) return this; } if (!this.min && !this.max) { this.min = new Point(min2); this.max = new Point(max2); } else { this.min.x = Math.min(min2.x, this.min.x); this.max.x = Math.max(max2.x, this.max.x); this.min.y = Math.min(min2.y, this.min.y); this.max.y = Math.max(max2.y, this.max.y); } return this; } getCenter(round) { return new Point((this.min.x + this.max.x) / 2, (this.min.y + this.max.y) / 2, round); } getBottomLeft() { return new Point(this.min.x, this.max.y); } getTopRight() { return new Point(this.max.x, this.min.y); } getTopLeft() { return this.min; } getBottomRight() { return this.max; } getSize() { return this.max.subtract(this.min); } contains(obj) { let min, max; if (typeof obj[0] === "number" || obj instanceof Point) obj = new Point(obj); else obj = new Bounds(obj); if (obj instanceof Bounds) { min = obj.min; max = obj.max; } else min = max = obj; return min.x >= this.min.x && max.x <= this.max.x && min.y >= this.min.y && max.y <= this.max.y; } intersects(bounds) { bounds = new Bounds(bounds); const min = this.min, max = this.max, min2 = bounds.min, max2 = bounds.max, xIntersects = max2.x >= min.x && min2.x <= max.x, yIntersects = max2.y >= min.y && min2.y <= max.y; return xIntersects && yIntersects; } overlaps(bounds) { bounds = new Bounds(bounds); const min = this.min, max = this.max, min2 = bounds.min, max2 = bounds.max, xOverlaps = max2.x > min.x && min2.x < max.x, yOverlaps = max2.y > min.y && min2.y < max.y; return xOverlaps && yOverlaps; } isValid() { return !!(this.min && this.max); } pad(bufferRatio) { const min = this.min, max = this.max, heightBuffer = Math.abs(min.x - max.x) * bufferRatio, widthBuffer = Math.abs(min.y - max.y) * bufferRatio; return new Bounds(new Point(min.x - heightBuffer, min.y - widthBuffer), new Point(max.x + heightBuffer, max.y + widthBuffer)); } equals(bounds) { if (!bounds) return false; bounds = new Bounds(bounds); return this.min.equals(bounds.getTopLeft()) && this.max.equals(bounds.getBottomRight()); } }; //#endregion export { Bounds }; //# sourceMappingURL=Bounds.js.map