aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-29 21:58:06 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-29 21:58:06 +0300
commit893bd10dd20f9adeef1735b1a7a1b68bb18f7096 (patch)
tree19947320c9f7a4db72cf68c8f9b7a2d4300e3e43
parenta3d42f5b23df682e3386940b4d92632f9f4c60db (diff)
downloadastar-893bd10dd20f9adeef1735b1a7a1b68bb18f7096.tar.xz
Add a path_length() function
-rw-r--r--path.c16
-rw-r--r--path.h2
2 files changed, 18 insertions, 0 deletions
diff --git a/path.c b/path.c
index ea2b4e0..3ea350c 100644
--- a/path.c
+++ b/path.c
@@ -258,6 +258,22 @@ size_t diagonal_distance(Position a, Position b) {
return (size_t) hypot(one, two);
}
+size_t path_length(Path path, Position start, Position goal) {
+ size_t length = 0;
+ if (path != NULL) {
+ Position cur = goal;
+ while (cur.x != start.x || cur.y != start.y) {
+ if (cur.x - path[cur.y][cur.x].parent.x == 0 || cur.y - path[cur.y][cur.x].parent.y == 0) {
+ length += COST_ORTHOGONAL;
+ } else {
+ length += COST_DIAGONAL;
+ }
+ cur = path[cur.y][cur.x].parent;
+ }
+ }
+ return length;
+}
+
void path_free(Path path, size_t height) {
if (path == NULL) return;
for (size_t i = 0; i < height; i++) {
diff --git a/path.h b/path.h
index 37c410a..12cfa80 100644
--- a/path.h
+++ b/path.h
@@ -14,6 +14,8 @@ size_t diagonal_distance(Position a, Position b);
void path_free(Path path, size_t height);
+size_t path_length(Path path, Position start, Position goal);
+
/* Helper funcs for the visited array */
char **visited_new(size_t width, size_t height);
void visited_clear(char **visited, size_t width, size_t height);