🚀 add test for path to pose + fix path to pose
This commit is contained in:
19
packages/compute/__tests__/getPathToPose.spec.ts
Normal file
19
packages/compute/__tests__/getPathToPose.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createSnakeFromCells } from "@snk/types/snake";
|
||||
import { getPathToPose } from "../getPathToPose";
|
||||
|
||||
it("should fing path to pose", () => {
|
||||
const snake0 = createSnakeFromCells([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 0 },
|
||||
]);
|
||||
const target = createSnakeFromCells([
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 0 },
|
||||
{ x: 3, y: 0 },
|
||||
]);
|
||||
|
||||
const path = getPathToPose(snake0, target);
|
||||
|
||||
expect(path).toBeDefined();
|
||||
});
|
||||
@@ -24,6 +24,8 @@ const isEmptySafe = (grid: Grid, x: number, y: number) =>
|
||||
|
||||
type M = { snake: Snake; parent: M | null; w: number; f: number };
|
||||
export const getPathToPose = (snake0: Snake, target: Snake, grid?: Grid) => {
|
||||
if (snakeEquals(snake0, target)) return [];
|
||||
|
||||
const targetCells = snakeToCells(target).reverse();
|
||||
|
||||
const snakeN = getSnakeLength(snake0);
|
||||
@@ -38,7 +40,9 @@ export const getPathToPose = (snake0: Snake, target: Snake, grid?: Grid) => {
|
||||
},
|
||||
};
|
||||
|
||||
const [t0] = targetCells;
|
||||
const [t0, ...forbidden] = targetCells;
|
||||
|
||||
forbidden.slice(0, 3);
|
||||
|
||||
const openList: M[] = [{ snake: snake0, w: 0 } as any];
|
||||
const closeList: Snake[] = [];
|
||||
@@ -76,7 +80,8 @@ export const getPathToPose = (snake0: Snake, target: Snake, grid?: Grid) => {
|
||||
: box.min.x <= nx &&
|
||||
nx <= box.max.x &&
|
||||
box.min.y <= ny &&
|
||||
ny <= box.max.y)
|
||||
ny <= box.max.y) &&
|
||||
!forbidden.some((p) => p.x === nx && p.y === ny)
|
||||
) {
|
||||
const snake = nextSnake(o.snake, dx, dy);
|
||||
|
||||
|
||||
41
packages/demo/demo.getPathToPose.ts
Normal file
41
packages/demo/demo.getPathToPose.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import "./menu";
|
||||
import { createCanvas } from "./canvas";
|
||||
import { createSnakeFromCells, snakeToCells } from "@snk/types/snake";
|
||||
import { grid, snake } from "./sample";
|
||||
import { getPathToPose } from "@snk/compute/getPathToPose";
|
||||
|
||||
const { canvas, ctx, draw, highlightCell } = createCanvas(grid);
|
||||
canvas.style.pointerEvents = "auto";
|
||||
|
||||
const target = createSnakeFromCells(
|
||||
snakeToCells(snake).map((p) => ({ ...p, x: p.x - 1 }))
|
||||
);
|
||||
|
||||
let chain = [snake, ...getPathToPose(snake, target)!];
|
||||
|
||||
let i = 0;
|
||||
const onChange = () => {
|
||||
ctx.clearRect(0, 0, 9999, 9999);
|
||||
|
||||
draw(grid, chain[i], []);
|
||||
chain
|
||||
.map(snakeToCells)
|
||||
.flat()
|
||||
.forEach(({ x, y }) => highlightCell(x, y));
|
||||
};
|
||||
|
||||
onChange();
|
||||
|
||||
const inputI = document.createElement("input") as any;
|
||||
inputI.type = "range";
|
||||
inputI.value = 0;
|
||||
inputI.max = chain ? chain.length - 1 : 0;
|
||||
inputI.step = 1;
|
||||
inputI.min = 0;
|
||||
inputI.style.width = "90%";
|
||||
inputI.style.padding = "20px 0";
|
||||
inputI.addEventListener("input", () => {
|
||||
i = +inputI.value;
|
||||
onChange();
|
||||
});
|
||||
document.body.append(inputI);
|
||||
@@ -1 +1,9 @@
|
||||
["interactive", "getBestTunnel", "getBestRoute", "outside", "getPathTo", "svg"]
|
||||
[
|
||||
"interactive",
|
||||
"getBestTunnel",
|
||||
"getBestRoute",
|
||||
"outside",
|
||||
"getPathToPose",
|
||||
"getPathTo",
|
||||
"svg"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user