UNPKG

@rxflow/manhattan

Version:

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

54 lines (50 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDirectionAngle = getDirectionAngle; exports.getDirectionChange = getDirectionChange; exports.getGridOffsets = getGridOffsets; var _geometry = require("../geometry"); var _resolver = require("../options/resolver"); /** * Fix angle end point to account for grid deformation */ function fixAngleEnd(start, end, grid, options) { const step = options.step; const diffX = end.x - start.x; const diffY = end.y - start.y; const gridStepsX = diffX / grid.x; const gridStepsY = diffY / grid.y; const distanceX = gridStepsX * step; const distanceY = gridStepsY * step; return new _geometry.Point(start.x + distanceX, start.y + distanceY); } /** * Get direction angle from start point to end point * Corrects for grid deformation between start and end */ function getDirectionAngle(start, end, directionCount, grid, options) { const quadrant = 360 / directionCount; const angleTheta = start.theta(fixAngleEnd(start, end, grid, options)); const normalizedAngle = (0, _resolver.normalizeAngle)(angleTheta + quadrant / 2); return quadrant * Math.floor(normalizedAngle / quadrant); } /** * Get the change in direction between two direction angles */ function getDirectionChange(angle1, angle2) { const change = Math.abs(angle1 - angle2); return change > 180 ? 360 - change : change; } /** * Fix direction offsets according to current grid */ function getGridOffsets(grid, options) { const step = options.step; options.directions.forEach(direction => { direction.gridOffsetX = direction.offsetX / step * grid.x; direction.gridOffsetY = direction.offsetY / step * grid.y; }); return options.directions; }