UNPKG

orthogonal-path-finding

Version:

Compute an orthogonal line from start to end with obstacles

164 lines (156 loc) 5.05 kB
declare enum DiagonalMovement { always = 1, never = 2, ifAtMostOneObstacle = 3, onlyWhenNoObstacles = 4 } /** * The Grid class, which serves as the encapsulation of the layout of the nodes. */ declare class Grid { width: number; height: number; nodes: Node_2[][]; /** * @constructor * @param {number | Array<Array<number | boolean>>} width_or_matrix - Number of columns of the grid, or matrix * @param {number} [height] - Number of rows of the grid. * @param {Array<Array<number | boolean>>} [matrix] - A 0-1 matrix representing the walkable status of the nodes * (0 or false for walkable). If the matrix is not supplied, all the nodes will be walkable. */ constructor(width_or_matrix: number | Array<Array<number | boolean>>, height?: number, matrix?: Array<Array<number | boolean>>); /** * Build and return the nodes. * @private * @param {number} width * @param {number} height * @param {Array<Array<number | boolean>>} [matrix] - A 0-1 matrix representing the walkable status of the nodes. * @see Grid */ private _buildNodes; getNodeAt(x: number, y: number): Node_2; /** * Determine whether the node at the given position is walkable. * (Also returns false if the position is outside the grid.) * @param {number} x - The x coordinate of the node. * @param {number} y - The y coordinate of the node. * @return {boolean} - The walkability of the node. */ isWalkableAt(x: number, y: number): boolean; /** * Determine whether the position is inside the grid. * XXX: `grid.isInside(x, y)` is weird to read. * It should be `(x, y) is inside grid`, but I failed to find a better * name for this method. * @param {number} x * @param {number} y * @return {boolean} */ isInside(x: number, y: number): boolean; /** * Set whether the node on the given position is walkable. * NOTE: throws exception if the coordinate is not inside the grid. * @param {number} x - The x coordinate of the node. * @param {number} y - The y coordinate of the node. * @param {boolean} walkable - Whether the position is walkable. */ setWalkableAt(x: number, y: number, walkable: boolean): void; /** * Get the neighbors of the given node. * * offsets diagonalOffsets: * +---+---+---+ +---+---+---+ * | | 0 | | | 0 | | 1 | * +---+---+---+ +---+---+---+ * | 3 | | 1 | | | | | * +---+---+---+ +---+---+---+ * | | 2 | | | 3 | | 2 | * +---+---+---+ +---+---+---+ * * When allowDiagonal is true, if offsets[i] is valid, then * diagonalOffsets[i] and * diagonalOffsets[(i + 1) % 4] is valid. * @param {Node} node * @param {DiagonalMovement} diagonalMovement */ getNeighbors(node: Node_2, diagonalMovement: DiagonalMovement): Node_2[]; /** * Get a clone of this grid. * @return {Grid} Cloned grid. */ clone(): Grid; } /** * A node in grid. * This class holds some basic information about a node and custom * attributes may be added, depending on the algorithms' needs. */ declare class Node_2 { /** * The x coordinate of the node on the grid. */ x: number; /** * The y coordinate of the node on the grid. */ y: number; /** * Whether this node can be walked through. */ walkable?: boolean; f: number; g: number; h: number; t: number; opened: boolean; closed: boolean; parent?: Node_2; /** * @constructor * @param {number} x - The x coordinate of the node on the grid. * @param {number} y - The y coordinate of the node on the grid. * @param {boolean} [walkable] - Whether this node is walkable. */ constructor(x: number, y: number, walkable?: boolean); } /** * Calculating Orthogonal Routing * @param start start point * @param end end point * @param obstacles obstacles (represented by rectangles) * @param options * @returns path points and a d for svg path render */ export declare const orthogonalPathFinding: (start: Point, end: Point, obstacles: Rectangle[], options?: PathFindingOptions) => { path: Point[]; d: string; walkablePoints?: Point[]; xs?: number[]; ys?: number[]; indexGrid?: Grid; startIndexPoint?: { x: number; y: number; }; endIndexPoint?: { x: number; y: number; }; xCenters?: number[]; yCenters?: number[]; }; declare type PathFindingOptions = { debug?: boolean; padding?: number; }; export declare type Point = { x: number; y: number; }; export declare type Rectangle = { x: number; y: number; width: number; height: number; }; export { }