aboutsummaryrefslogtreecommitdiff
path: root/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'path.c')
-rw-r--r--path.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/path.c b/path.c
index 450ea91..f604ec1 100644
--- a/path.c
+++ b/path.c
@@ -337,14 +337,19 @@ void path_free(Path path, size_t height) {
}
void path_reverse(Path *path, size_t width, size_t height, Position *start, Position *end) {
- Path new_path = path_new(width, height);
- if (new_path == NULL) error("Failed to allocate path\n");
-
- 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;
+ if (*path != NULL) {
+ Path new_path = path_new(width, height);
+ if (new_path == NULL) error("Failed to allocate path\n");
+
+ 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;
+ }
+
+ path_free(*path, height);
+ *path = new_path;
}
/* Switch start and end around */
@@ -356,9 +361,6 @@ void path_reverse(Path *path, size_t width, size_t height, Position *start, Posi
end->y ^= start->y;
start->y ^= end->y;
- path_free(*path, height);
- *path = new_path;
-
return;
}