aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c6
-rw-r--r--path.c23
-rw-r--r--path.h2
3 files changed, 31 insertions, 0 deletions
diff --git a/main.c b/main.c
index 4347551..93dfc1e 100644
--- a/main.c
+++ b/main.c
@@ -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':
diff --git a/path.c b/path.c
index d643167..19a9aaa 100644
--- a/path.c
+++ b/path.c
@@ -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;
diff --git a/path.h b/path.h
index ea812e8..4a2159f 100644
--- a/path.h
+++ b/path.h
@@ -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);