UNPKG

fqtree

Version:

a flexible quadtree for JavaScript/TypeScript

256 lines 7.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LineRange = exports.CircleRange = exports.RectangleRange = void 0; var lodash_1 = require("lodash"); var evjkit_1 = require("evjkit"); var csclip_1 = require("./csclip"); var RectangleRange = /** @class */ (function () { /** * Represents a Rectangular Range. * @param x - x-coordinate of rectangle's center. * @param y - y-coordinate of rectangle's center. * @param w - half-width of the rectangle. * @param h - half-height of the rectangle. */ function RectangleRange(x, y, w, h) { this.x = x; this.y = y; this.w = Math.abs(w); this.h = Math.abs(h); } Object.defineProperty(RectangleRange.prototype, "left", { get: function () { return this.x - this.w; }, enumerable: false, configurable: true }); Object.defineProperty(RectangleRange.prototype, "right", { get: function () { return this.x + this.w; }, enumerable: false, configurable: true }); Object.defineProperty(RectangleRange.prototype, "top", { get: function () { return this.y - this.h; }, enumerable: false, configurable: true }); Object.defineProperty(RectangleRange.prototype, "bottom", { get: function () { return this.y + this.h; }, enumerable: false, configurable: true }); Object.defineProperty(RectangleRange.prototype, "bounds", { get: function () { return { x: this.x, y: this.y, w: this.w, h: this.h, }; }, enumerable: false, configurable: true }); /** * tests if a point is within this range. * @param point - point to test. * @returns true if within, false if without. */ RectangleRange.prototype.containsPoint = function (point) { return (point.x >= this.left && point.x <= this.right && point.y >= this.top && point.y <= this.bottom); }; /** * tests if a range is within this range. * @param range - range to test. * @returns true if within, false if without. */ RectangleRange.prototype.containsRange = function (range) { return (range.left > this.left && range.right < this.right && range.top > this.top && range.bottom < this.bottom); }; /** * tests if a shape is within or partially within this range. * @param bounds - bounds to test. * @returns true if intersection exists, otherwise false. */ RectangleRange.prototype.intersects = function (bounds) { return !(bounds.left > this.right || bounds.right < this.left || bounds.top > this.bottom || bounds.bottom < this.top); }; return RectangleRange; }()); exports.RectangleRange = RectangleRange; var CircleRange = /** @class */ (function () { /** * represents a circular area range. * @param x - x-coordiante of circle center. * @param y - y-coordinate of circle center * @param r - radius of circle. */ function CircleRange(x, y, r) { this.x = x; this.y = y; this.r = r; this.r2 = r * r; this.w = r; this.h = r; } Object.defineProperty(CircleRange.prototype, "left", { get: function () { return this.x - this.r; }, enumerable: false, configurable: true }); Object.defineProperty(CircleRange.prototype, "right", { get: function () { return this.x + this.r; }, enumerable: false, configurable: true }); Object.defineProperty(CircleRange.prototype, "top", { get: function () { return this.y - this.r; }, enumerable: false, configurable: true }); Object.defineProperty(CircleRange.prototype, "bottom", { get: function () { return this.y + this.r; }, enumerable: false, configurable: true }); Object.defineProperty(CircleRange.prototype, "bounds", { get: function () { return { x: this.x, y: this.y, w: this.r, h: this.r, r: this.r, }; }, enumerable: false, configurable: true }); /** * tests if a point is within this range. * @param point - point to test. * @returns true if within, false if without. */ CircleRange.prototype.containsPoint = function (point) { var distSq = evjkit_1.Vector2.fromPoint(this).distanceToSq(evjkit_1.Vector2.fromPoint(point)); return distSq < this.r2; }; /** * tests if a shape is within or partially within this range. * @param bounds - bounds to test. * @returns true if intersection exists, otherwise false. */ CircleRange.prototype.intersects = function (bounds) { // Find the closest point to the circle within the rectangle var minX = (0, lodash_1.clamp)(this.x, bounds.left, bounds.right); var minY = (0, lodash_1.clamp)(this.y, bounds.top, bounds.bottom); var distSq = evjkit_1.Vector2.fromPoint(this).distanceToSq(new evjkit_1.Vector2(minX, minY)); return distSq < this.r2; }; return CircleRange; }()); exports.CircleRange = CircleRange; var LineRange = /** @class */ (function () { /** * represents a line area range. * @param from - starting point of line * @param to - ending point of line */ function LineRange(from, to) { this.from = from; this.to = to; } Object.defineProperty(LineRange.prototype, "left", { get: function () { if (this.from.x < this.to.x) return this.from.x; else return this.to.x; }, enumerable: false, configurable: true }); Object.defineProperty(LineRange.prototype, "right", { get: function () { if (this.from.x > this.to.x) return this.from.x; else return this.to.x; }, enumerable: false, configurable: true }); Object.defineProperty(LineRange.prototype, "top", { get: function () { if (this.from.y < this.to.y) return this.from.y; else return this.to.y; }, enumerable: false, configurable: true }); Object.defineProperty(LineRange.prototype, "bottom", { get: function () { if (this.from.y > this.to.y) return this.from.y; else return this.to.y; }, enumerable: false, configurable: true }); Object.defineProperty(LineRange.prototype, "bounds", { get: function () { var left = this.left; var right = this.right; var top = this.top; var bottom = this.bottom; return { x: left, y: top, w: right - left, h: bottom - top, from: this.from, to: this.to, }; }, enumerable: false, configurable: true }); /** * tests if a shape is within or partially within this range. * @param bounds - bounds to test. * @returns true if intersection exists, otherwise false. */ LineRange.prototype.intersects = function (bounds) { return (0, csclip_1.csClip)(this, bounds); }; return LineRange; }()); exports.LineRange = LineRange; //# sourceMappingURL=range.js.map