API / misc/pathfinder
misc/pathfinder
그리드 길찾기. (A*)
- 원본 misc/pathfinder.js 를 옮긴 것이다. 4방향 이동, 맨해튼 거리 휴리스틱, 이진 최소 힙으로 열린 목록을 관리한다.
- is_blocked_handler(column, row) 가 참이면 막힌 칸이다.
- 결과는 시작 칸을 빼고 도착 칸까지의 칸 목록. 길이 없으면 None.
- 원본과 다른 점: { column, row } 는 GridCell, null 은 None 이다. 힙은 원본 코드를 그대로 옮겨서, 비용이 같은 길이 여럿일 때 원본과 같은 길을 고른다. (표준 BinaryHeap 은 고르는 순서가 다르다)
Grid pathfinding. (A*)
- A port of the original misc/pathfinder.js. 4-directional movement, Manhattan distance heuristic, and a binary min heap managing the open list.
- A cell is blocked if is_blocked_handler(column, row) is true.
- The result is the list of cells up to the goal cell, excluding the start cell. None if there is no path.
- Differences from the original:
{ column, row } is GridCell, and null is None.
The heap is ported as is from the original code, so when several paths have the same cost it picks the same path as the original. (the standard BinaryHeap picks in a different order)PathFinderstruct
그리드 길찾기. (정적 함수 묶음)
- 상태가 없다. find_path 를 바로 부른다.
Grid pathfinding. (a set of static functions)
- Stateless. Call find_path directly.
예제Example벽을 돌아가는 길A path around a wall
use vanilla::{GridCell, PathFinder};
let map = [
"....",
".##.",
".###",
];
let is_wall = |column: i32, row: i32| -> bool {
map[row as usize].as_bytes()[column as usize] == b'#'
};
let path = PathFinder::find_path(4, 3, GridCell::create(0, 1), GridCell::create(3, 1), is_wall).unwrap();
assert_eq!(path.len(), 5);
assert_eq!(path[0], GridCell::create(0, 0));
assert_eq!(*path.last().unwrap(), GridCell::create(3, 1));정적static
pub fn find_path(column_count: i32, row_count: i32, start: GridCell, goal: GridCell, mut is_blocked_handler: impl FnMut(i32, i32) -> bool) -> Option<Vec<GridCell>>#경로 찾기. (시작이나 도착이 밖이거나 도착이 막혔으면 None, 시작과 도착이 같으면 빈 목록)
- 4방향으로 한 칸에 비용 1 씩 움직이는 최단 경로. 시작 칸이 막혔는지는 보지 않는다.
Finds a path. (None if the start or goal is outside or the goal is blocked, an empty list if the start and goal are the same)
- Shortest path moving in 4 directions at a cost of 1 per cell. Does not check whether the start cell is blocked.
- column_counti32
- 그리드 열 수. (칸 번호는 0 ~ column_count - 1)Grid column count. (cell indexes are 0 ~ column_count - 1)
- row_counti32
- 그리드 행 수. (칸 번호는 0 ~ row_count - 1)Grid row count. (cell indexes are 0 ~ row_count - 1)
- startGridCell
- 시작 칸.Start cell.
- goalGridCell
- 도착 칸.Goal cell.
- is_blocked_handlerimpl FnMut(i32, i32) -> bool
- (열, 행) 칸이 막혔으면 true 를 돌려주는 콜백. 그리드 안의 칸에만 부른다.Callback that returns true if the (column, row) cell is blocked. Only called for cells inside the grid.
반환ReturnsOption<Vec<GridCell>> 시작 칸을 빼고 도착 칸까지 이웃끼리 이어진 칸 목록. 길이 없으면 None.List of adjacent connected cells up to the goal cell, excluding the start cell. None if there is no path.
짝Counterpart: vanilla.js PathFinder.findPath
예제Example막힌 곳이 없으면 곧은 길, 도착이 막히면 NoneA straight path with no obstacles, None if the goal is blocked
use vanilla::{GridCell, PathFinder};
let straight_path = PathFinder::find_path(3, 1, GridCell::create(0, 0), GridCell::create(2, 0), |_, _| false);
assert_eq!(straight_path, Some(vec![GridCell::create(1, 0), GridCell::create(2, 0)]));
let blocked_path = PathFinder::find_path(3, 1, GridCell::create(0, 0), GridCell::create(2, 0), |column, _| column == 2);
assert!(blocked_path.is_none());