UNPKG

@rxflow/manhattan

Version:

Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance

96 lines (90 loc) 4.2 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { Point } from "./Point"; /** * Line class representing a line segment */ export var Line = /*#__PURE__*/function () { function Line(start, end) { _classCallCheck(this, Line); _defineProperty(this, "start", void 0); _defineProperty(this, "end", void 0); this.start = start; this.end = end; } /** * Calculate intersection points with a rectangle * Returns an array of intersection points */ _createClass(Line, [{ key: "intersect", value: function intersect(rect) { var intersections = []; // Define rectangle edges var edges = [ // Top edge { p1: new Point(rect.x, rect.y), p2: new Point(rect.x + rect.width, rect.y) }, // Right edge { p1: new Point(rect.x + rect.width, rect.y), p2: new Point(rect.x + rect.width, rect.y + rect.height) }, // Bottom edge { p1: new Point(rect.x, rect.y + rect.height), p2: new Point(rect.x + rect.width, rect.y + rect.height) }, // Left edge { p1: new Point(rect.x, rect.y), p2: new Point(rect.x, rect.y + rect.height) }]; // Check intersection with each edge for (var _i = 0, _edges = edges; _i < _edges.length; _i++) { var edge = _edges[_i]; var intersection = this.lineIntersection(this.start, this.end, edge.p1, edge.p2); if (intersection) { intersections.push(intersection); } } return intersections; } /** * Calculate intersection point between two line segments * Returns null if lines don't intersect */ }, { key: "lineIntersection", value: function lineIntersection(p1, p2, p3, p4) { var x1 = p1.x, y1 = p1.y; var x2 = p2.x, y2 = p2.y; var x3 = p3.x, y3 = p3.y; var x4 = p4.x, y4 = p4.y; var denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); // Lines are parallel if (Math.abs(denom) < 1e-10) { return null; } var t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom; var u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom; // Check if intersection is within both line segments if (t >= 0 && t <= 1 && u >= 0 && u <= 1) { return new Point(x1 + t * (x2 - x1), y1 + t * (y2 - y1)); } return null; } }]); return Line; }();