From 5f39e2014b7f2dfe5db08828a4b8bc94cda6b152 Mon Sep 17 00:00:00 2001 From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 26 Dec 2022 23:19:07 -0500 Subject: [PATCH] graph display --- .gitignore | 5 ++++ dependencies.txt | 1 + display.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ graph.py | 41 ++++++++++++++++++++++++++++++++ main.py | 16 +++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 dependencies.txt create mode 100644 display.py create mode 100644 graph.py create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5159ca0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +.idea/ +__pycache__/ diff --git a/dependencies.txt b/dependencies.txt new file mode 100644 index 0000000..0cb7ff1 --- /dev/null +++ b/dependencies.txt @@ -0,0 +1 @@ +pygame diff --git a/display.py b/display.py new file mode 100644 index 0000000..bc7dea7 --- /dev/null +++ b/display.py @@ -0,0 +1,62 @@ +import pygame + + +pygame.init() + +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +GRAY = (128, 128, 128) +BLUE = (0, 0, 255) + + +class Node: + background_colour = GRAY + outline_colour = BLACK + text_colour = WHITE + + radius = 15 + thickness = 1 + font = pygame.font.SysFont("arial", 15) + + def __init__(self, position: tuple, number: int) -> None: + self.position = pygame.Vector2(position) + self.text = self.font.render(str(number), True, self.text_colour) + + def draw(self, surf: pygame.Surface) -> None: + pygame.draw.circle(surf, self.background_colour, self.position, self.radius) + pygame.draw.circle(surf, self.outline_colour, self.position, self.radius, width=self.thickness) + surf.blit(self.text, self.text.get_rect(center=self.position)) + + +class Display: + + def __init__(self, path: str) -> None: + with open(path, "r") as f: + contents = f.read().split("\n") + if contents[-1] == "": + contents = contents[:-1] + + self.WIDTH, self.HEIGHT, self.N = tuple(map(int, contents[0].split(" "))) + self.nodes = [Node(tuple(map(int, node.split(" "))), num) for num, node in enumerate(contents[1:])] + self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) + self.is_running = True + + self.clock = pygame.time.Clock() + self.delta = 0 + + def show(self, route: list) -> None: + while self.is_running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.is_running = False + + self.screen.fill(WHITE) + + if len(route) > 1: + pygame.draw.aalines(self.screen, BLUE, True, route, 5) + + for node in self.nodes: + node.draw(self.screen) + + pygame.display.update() + self.delta = self.clock.tick(60) diff --git a/graph.py b/graph.py new file mode 100644 index 0000000..2cf5f10 --- /dev/null +++ b/graph.py @@ -0,0 +1,41 @@ +import os +import random + + +def prune_filename(filename: str) -> int: + return int(filename[5:-4]) + + +def create(path: str, width: int, height: int, nodes: int): + graph = [] + text = f"{width} {height} {nodes}\n" + + for node in range(nodes): + x, y = (random.randint(0, width), random.randint(0, height)) + graph.append((x, y)) + text += f"{x} {y}\n" + + current_file = 0 + for root, dirs, files in os.walk("graphs/"): + for name in files: + current_file = max(current_file, prune_filename(name)) + filename = f"graph{current_file + 1}.txt" + with open(os.path.join(path, filename), "w") as f: + f.write(text[:-1]) + + return graph, filename + + +def distance(x1: int, x2: int, y1: int, y2: int) -> float: + return pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5) + + +def get_distances(graph: list) -> dict: + distances = dict() + for i in graph: + distances[i] = dict() + x1, y1 = i + for j in graph: + x2, y2 = j + distances[i][j] = distance(x1, x2, y1, y2) + return distances diff --git a/main.py b/main.py new file mode 100644 index 0000000..f504265 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +from graph import create +from display import Display +import os + +GRAPH_PATH = "graphs/" + + +def main(): + graph, filename = create(GRAPH_PATH, 640, 640, 20) + display = Display(os.path.join(GRAPH_PATH, filename)) + + display.show(graph) + + +if __name__ == "__main__": + main() -- 2.54.0