From 893bd10dd20f9adeef1735b1a7a1b68bb18f7096 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Sun, 29 Mar 2026 21:58:06 +0300 Subject: Add a path_length() function --- path.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'path.c') 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++) { -- cgit v1.2.3