diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-22 20:48:27 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-22 20:48:27 +0300 |
| commit | 55b10d144a6855791f3918f37ccdf36fd8109e58 (patch) | |
| tree | 788897d35fe3eec8751a3fd4c3958941126d8709 | |
| parent | 1cd0a30593d9b7805752e3e183c93f622211ed0b (diff) | |
| download | astar-55b10d144a6855791f3918f37ccdf36fd8109e58.tar.xz | |
Add a path_reverse() function
| -rw-r--r-- | main.c | 6 | ||||
| -rw-r--r-- | path.c | 23 | ||||
| -rw-r--r-- | path.h | 2 |
3 files changed, 31 insertions, 0 deletions
@@ -186,6 +186,8 @@ int main(int argc, char **argv) { * * [d] - Switch algorithms (A* or Dijsktra's) * + * [r] - Reverse the path and switch start/end around + * * [s] - Save the map to a bmp file * * [w] - Open a map from a plaintext file @@ -276,6 +278,10 @@ int main(int argc, char **argv) { /* TODO: print time */ break; + case 'r': + path_reverse(&path, width, height, &start_pos, &end_pos); + break; + case 'y': case 'o': case 'u': @@ -330,6 +330,29 @@ void path_free(Path path, size_t height) { free(path); } +void path_reverse(Path *path, size_t width, size_t height, Position *start, Position *end) { + Path new_path = path_new(width, height); + + Position cur = *end; + while (cur.x != start->x || cur.y != start->y) { + Position parent = (*path)[cur.y][cur.x]; + new_path[parent.y][parent.x] = cur; + cur = parent; + } + + /* Switch start and end around */ + start->x = start->x ^ end->x; + end->x = start->x ^ end->x; + start->x = start->x ^ end->x; + + start->y = start->y ^ end->y; + end->y = start->y ^ end->y; + start->y = start->y ^ end->y; + + path_free(*path, height); + *path = new_path; +} + char **visited_new(size_t width, size_t height) { char **visited = malloc(sizeof(char*) * height); if (visited == NULL) return NULL; @@ -19,6 +19,8 @@ size_t diagonal_distance(Position a, Position b); Path path_new(size_t width, size_t height); void path_free(Path path, size_t height); +/* Reverses the path in place and switches start and end */ +void path_reverse(Path *path, size_t width, size_t height, Position *start, Position *end); /* Helper funcs for the visited array */ char **visited_new(size_t width, size_t height); |
