From 542db7c952ab8b475fce88949dc40c7c2ee2fdf2 Mon Sep 17 00:00:00 2001 From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 26 Dec 2022 23:47:29 -0500 Subject: [PATCH] calculate distance for route func --- graph.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/graph.py b/graph.py index 2cf5f10..06a4705 100644 --- a/graph.py +++ b/graph.py @@ -16,7 +16,7 @@ def create(path: str, width: int, height: int, nodes: int): text += f"{x} {y}\n" current_file = 0 - for root, dirs, files in os.walk("graphs/"): + for root, dirs, files in os.walk(path): for name in files: current_file = max(current_file, prune_filename(name)) filename = f"graph{current_file + 1}.txt" @@ -39,3 +39,14 @@ def get_distances(graph: list) -> dict: x2, y2 = j distances[i][j] = distance(x1, x2, y1, y2) return distances + + +def calculate_distance(route: list) -> float: + x1, y1 = route[0] + x2, y2 = route[-1] + d = distance(x1, x2, y1, y2) + for i, node in enumerate(route[:-1]): + x2, y2 = route[i + 1] + d += distance(x1, x2, y1, y2) + x1, y1 = x2, y2 + return d -- 2.54.0