@rxflow/manhattan
Version:
Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance
117 lines (107 loc) • 4.29 kB
JavaScript
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); }
/**
* Point class representing a 2D coordinate
*/
export var Point = /*#__PURE__*/function () {
function Point(x, y) {
_classCallCheck(this, Point);
_defineProperty(this, "x", void 0);
_defineProperty(this, "y", void 0);
this.x = x;
this.y = y;
}
/**
* Create a copy of this point
*/
_createClass(Point, [{
key: "clone",
value: function clone() {
return new Point(this.x, this.y);
}
/**
* Check if this point equals another point
*/
}, {
key: "equals",
value: function equals(other) {
return this.x === other.x && this.y === other.y;
}
/**
* Translate this point by dx and dy
*/
}, {
key: "translate",
value: function translate(dx, dy) {
return new Point(this.x + dx, this.y + dy);
}
/**
* Round coordinates to specified precision
*/
}, {
key: "round",
value: function round(precision) {
var factor = Math.pow(10, precision);
return new Point(Math.round(this.x * factor) / factor, Math.round(this.y * factor) / factor);
}
/**
* Calculate Manhattan distance to another point
*/
}, {
key: "manhattanDistance",
value: function manhattanDistance(other) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
}
/**
* Calculate angle (in degrees) from this point to another point
*/
}, {
key: "theta",
value: function theta(other) {
var dx = other.x - this.x;
var dy = other.y - this.y;
var radians = Math.atan2(dy, dx);
return radians * 180 / Math.PI;
}
/**
* Calculate the difference vector from this point to another
*/
}, {
key: "diff",
value: function diff(other) {
return new Point(other.x - this.x, other.y - this.y);
}
/**
* Convert point to string representation
*/
}, {
key: "toString",
value: function toString() {
return "".concat(this.x, "@").concat(this.y);
}
/**
* Snap point to grid
*/
}, {
key: "snapToGrid",
value: function snapToGrid(gridSize) {
return new Point(Math.round(this.x / gridSize) * gridSize, Math.round(this.y / gridSize) * gridSize);
}
/**
* Calculate squared distance to another point (for performance)
*/
}, {
key: "squaredDistance",
value: function squaredDistance(other) {
var dx = other.x - this.x;
var dy = other.y - this.y;
return dx * dx + dy * dy;
}
}]);
return Point;
}();