UNPKG

@rxflow/manhattan

Version:

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

94 lines (83 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Point = void 0; /** * Point class representing a 2D coordinate */ class Point { x; y; constructor(x, y) { this.x = x; this.y = y; } /** * Create a copy of this point */ clone() { return new Point(this.x, this.y); } /** * Check if this point equals another point */ equals(other) { return this.x === other.x && this.y === other.y; } /** * Translate this point by dx and dy */ translate(dx, dy) { return new Point(this.x + dx, this.y + dy); } /** * Round coordinates to specified precision */ round(precision) { const 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 */ 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 */ theta(other) { const dx = other.x - this.x; const dy = other.y - this.y; const radians = Math.atan2(dy, dx); return radians * 180 / Math.PI; } /** * Calculate the difference vector from this point to another */ diff(other) { return new Point(other.x - this.x, other.y - this.y); } /** * Convert point to string representation */ toString() { return `${this.x}@${this.y}`; } /** * Snap point to grid */ 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) */ squaredDistance(other) { const dx = other.x - this.x; const dy = other.y - this.y; return dx * dx + dy * dy; } } exports.Point = Point;