🚀 add test for path to pose + fix path to pose

This commit is contained in:
platane
2020-11-03 22:33:24 +01:00
parent 2d1d70a10c
commit cd458e61d3
4 changed files with 76 additions and 3 deletions

View 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();
});

View File

@@ -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);

View 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);

View File

@@ -1 +1,9 @@
["interactive", "getBestTunnel", "getBestRoute", "outside", "getPathTo", "svg"]
[
"interactive",
"getBestTunnel",
"getBestRoute",
"outside",
"getPathToPose",
"getPathTo",
"svg"
]