|
1 | 1 | # AStarD |
2 | | -A* (A-Star) search algorithm for 2D spaces for D. |
| 2 | +A* (A-Star) search algorithm for 2D spaces (grids) with straightforward/diagonal movement with 4 available heuristics: |
| 3 | +- **Manhattan** |
| 4 | +- **Euclidean** |
| 5 | +- **Octile** |
| 6 | +- **Chebyshev** |
| 7 | + |
| 8 | +AStarD is extremely easy to use, while its performance fits within the average A* execution time. |
| 9 | + |
| 10 | +<p align="center"><img src="https://github.com/user-attachments/assets/8f7e315c-2bba-4262-b8a0-174234545af0" width="600"><br><code>[[0, 0], [1, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 8], [9, 8]]</code></p> |
| 11 | + |
| 12 | +```d |
| 13 | +import AStarD.AStar; |
| 14 | +
|
| 15 | +void main() { |
| 16 | + int[][] grid = [ |
| 17 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0], |
| 18 | + [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0], |
| 19 | + [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0], |
| 20 | + [0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], |
| 21 | + [0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0], |
| 22 | + [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0], |
| 23 | + [0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1], |
| 24 | + [0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0], |
| 25 | + [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], |
| 26 | + [0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0], |
| 27 | + [1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1], |
| 28 | + [0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1], |
| 29 | + [0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1], |
| 30 | + [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], |
| 31 | + [1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0], |
| 32 | + [0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0] |
| 33 | + ]; |
| 34 | +
|
| 35 | + AStar(grid, [0, 0], [9, 8], Heuristic.MANHATTAN, true); // Returns [[0, 0], [1, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 8], [9, 8]] |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +**`AStar` signature:<br>`ASPath ASter(Grid2D p_Grid, Position2D p_Start, Position2D p_Target, Heuristic p_Heuristic, bool p_AllowDiagonalMovement)`<br>Where `ASPath` is `int[2][]`,<br>`Grid2D` is `int[][]`,<br>`Position2D` is `int[2]`.** |
0 commit comments