UNPKG

d3plus-shape

Version:

Fancy SVG shapes for visualizations

57 lines (56 loc) 3.7 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import lineIntersection from "./lineIntersection.js"; import segmentBoxContains from "./segmentBoxContains.js"; import pointDistanceSquared from "./pointDistanceSquared.js"; /** @function polygonRayCast @desc Gives the two closest intersection points between a ray cast from a point inside a polygon. The two points should lie on opposite sides of the origin. @param {Array} poly The polygon to test against, which should be an `[x, y]` formatted Array. @param {Array} origin The origin point of the ray to be cast, which should be an `[x, y]` formatted Array. @param {Number} [alpha = 0] The angle in radians of the ray. @returns {Array} An array containing two values, the closest point on the left and the closest point on the right. If either point cannot be found, that value will be `null`. */ export default function (poly, origin) { var alpha = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; var eps = 1e-9; origin = [origin[0] + eps * Math.cos(alpha), origin[1] + eps * Math.sin(alpha)]; var _origin = origin, _origin2 = _slicedToArray(_origin, 2), x0 = _origin2[0], y0 = _origin2[1]; var shiftedOrigin = [x0 + Math.cos(alpha), y0 + Math.sin(alpha)]; var idx = 0; if (Math.abs(shiftedOrigin[0] - x0) < eps) idx = 1; var i = -1; var n = poly.length; var b = poly[n - 1]; var minSqDistLeft = Number.MAX_VALUE; var minSqDistRight = Number.MAX_VALUE; var closestPointLeft = null; var closestPointRight = null; while (++i < n) { var a = b; b = poly[i]; var p = lineIntersection(origin, shiftedOrigin, a, b); if (p && segmentBoxContains(a, b, p)) { var sqDist = pointDistanceSquared(origin, p); if (p[idx] < origin[idx]) { if (sqDist < minSqDistLeft) { minSqDistLeft = sqDist; closestPointLeft = p; } } else if (p[idx] > origin[idx]) { if (sqDist < minSqDistRight) { minSqDistRight = sqDist; closestPointRight = p; } } } } return [closestPointLeft, closestPointRight]; }