UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

119 lines (118 loc) • 2.59 kB
import { formatNum } from "../core/Util.js"; //#region src/geometry/Point.js var Point = class Point { constructor(x, y, round) { if (!Point.validate(x, y)) throw new Error(`Invalid Point object: (${x}, ${y})`); let _x, _y; if (x instanceof Point) { _x = x.x; _y = x.y; } else if (Array.isArray(x)) { _x = x[0]; _y = x[1]; } else if (typeof x === "object" && "x" in x && "y" in x) { _x = x.x; _y = x.y; } else { _x = x; _y = y; } this.x = round ? Math.round(_x) : _x; this.y = round ? Math.round(_y) : _y; } static validate(x, y) { if (x instanceof Point || Array.isArray(x)) return true; else if (x && typeof x === "object" && "x" in x && "y" in x) return true; else if ((x || x === 0) && (y || y === 0)) return true; return false; } add(point) { return new Point(this)._add(new Point(point)); } _add(point) { this.x += point.x; this.y += point.y; return this; } subtract(point) { return new Point(this)._subtract(new Point(point)); } _subtract(point) { this.x -= point.x; this.y -= point.y; return this; } divideBy(num) { return new Point(this)._divideBy(num); } _divideBy(num) { this.x /= num; this.y /= num; return this; } multiplyBy(num) { return new Point(this)._multiplyBy(num); } _multiplyBy(num) { this.x *= num; this.y *= num; return this; } scaleBy(point) { return new Point(this.x * point.x, this.y * point.y); } unscaleBy(point) { return new Point(this.x / point.x, this.y / point.y); } round() { return new Point(this)._round(); } _round() { this.x = Math.round(this.x); this.y = Math.round(this.y); return this; } floor() { return new Point(this)._floor(); } _floor() { this.x = Math.floor(this.x); this.y = Math.floor(this.y); return this; } ceil() { return new Point(this)._ceil(); } _ceil() { this.x = Math.ceil(this.x); this.y = Math.ceil(this.y); return this; } trunc() { return new Point(this)._trunc(); } _trunc() { this.x = Math.trunc(this.x); this.y = Math.trunc(this.y); return this; } distanceTo(point) { point = new Point(point); const x = point.x - this.x, y = point.y - this.y; return Math.sqrt(x * x + y * y); } equals(point) { point = new Point(point); return point.x === this.x && point.y === this.y; } contains(point) { point = new Point(point); return Math.abs(point.x) <= Math.abs(this.x) && Math.abs(point.y) <= Math.abs(this.y); } toString() { return `Point(${formatNum(this.x)}, ${formatNum(this.y)})`; } }; //#endregion export { Point }; //# sourceMappingURL=Point.js.map