]> Skullheadx's Git Forge - The-Traveling-Salesman-Problem.git/commitdiff
fix glitch where 2 points on same spot
authorSkullheadx <704277@pdsb.net>
Wed, 28 Dec 2022 00:37:09 +0000 (19:37 -0500)
committerSkullheadx <704277@pdsb.net>
Wed, 28 Dec 2022 00:37:09 +0000 (19:37 -0500)
graph.py

index d30f652ac5e702271964039158bb8aca81e16436..7fab2f565e2ef4e2bfd0169213dc4158894770e4 100644 (file)
--- a/graph.py
+++ b/graph.py
@@ -9,11 +9,16 @@ def prune_filename(filename: str) -> int:
 def create(path: str, width: int, height: int, nodes: int):
     graph = []
     text = f"{width} {height} {nodes}\n"
+    seen = set()  # so that there are no duplicate points
 
-    for node in range(nodes):
+    counter = 0
+    while counter < nodes:
         x, y = (random.randint(0, width), random.randint(0, height))
-        graph.append((x, y))
-        text += f"{x} {y}\n"
+        if (x, y) not in seen:
+            graph.append((x, y))
+            text += f"{x} {y}\n"
+            seen.add((x, y))
+            counter += 1
 
     current_file = 0
     for root, dirs, files in os.walk(path):