generic-astar
v1.0.0
Published
Generic JS/TS A* implementation. Comes with helper utils!
Readme
generic-astar
Generic JS/TS A* implementation. Comes with helper utils!
Installation
# Any of the following
npm install generic-astar
yarn add generic-astar
bun add generic-astarUsage
TypeScript
import AStar from 'generic-astar';
import { GridAdapter } from 'generic-astar/adapters';
const adapter = new GridAdapter(
{ x: 5, y: 5 },
{
blocked: [[1, 1]],
target: [[4, 4]],
diagonals: true,
}
);
const result = AStar({ from: [0, 0], adapter });import AStar from 'generic-astar';
const result = AStar<[number, number]>({
from: [0, 0],
to: [[4, 4]],
hash: node => node.join(','),
heuristic: node => Math.sqrt((4 - node[0]) ** 2 + (4 - node[1]) ** 2),
neighbors: node => {
if (node[0] === 0 && node[1] === 0)
return [
{ node: [3, 0], cost: 3 },
{ node: [0, 2], cost: 2 },
];
if (node[0] === 4) return [{ node: [4, 4], cost: Math.abs(node[1] - 4) }];
return [{ node: [node[0] + 1, node[1] + 1], cost: Math.sqrt(2) }];
},
});
// ({
// distance: 4 + 4 * SQRT2,
// path: [
// [0, 0],
// [0, 2],
// [1, 3],
// [2, 4],
// [3, 5],
// [4, 6],
// [4, 4],
// ],
// });JavaScript (ESM)
import AStar from 'generic-astar';JavaScript (CJS)
const AStar = require('generic-astar');