changed auto length calculation

This commit is contained in:
Louis Heredero 2024-07-01 23:33:08 +02:00
parent efc88658ee
commit 691cb7da73
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 20 additions and 10 deletions

View File

@ -429,9 +429,7 @@ class Editor:
self.select_node(self.graph.number_of_nodes() - 1)
def create_edge(self, node_1: int, node_2: int) -> None:
n1 = self.graph.nodes[node_1]
n2 = self.graph.nodes[node_2]
self.graph.add_edge(node_1, node_2, ((n1.x - n2.x) ** 2 + (n1.z - n2.z) ** 2) ** 0.5)
self.graph.add_edge(node_1, node_2)
def get_hovering_nodes(self) -> tuple[list[int], list[float]]:
hovering = []

View File

@ -1,5 +1,5 @@
from __future__ import annotations
from math import inf
from math import inf, sqrt
from typing import Iterator, Optional
from src.graph.node import Node
@ -14,7 +14,11 @@ class Graph:
def add_node(self, x: int, z: int, name: str = "") -> None:
self.nodes.append(Node(x, z, len(self.nodes), name))
def add_edge(self, start_index: int, end_index: int, length: float) -> None:
def add_edge(self, start_index: int, end_index: int, auto_length: bool = True) -> None:
length = 0
if auto_length:
n1, n2 = self.nodes[start_index], self.nodes[end_index]
length = sqrt((n1.x - n2.x)**2 + (n1.z - n2.z)**2)
self.edges.append(Edge(start_index, end_index, length, len(self.edges)))
def delete_edge(self, edge: Edge) -> None:
@ -38,6 +42,12 @@ class Graph:
for no in self.nodes:
no.index = self.nodes.index(no)
def recompute_lengths(self) -> None:
for edge in self.edges:
n1 = self.nodes[edge.start]
n2 = self.nodes[edge.end]
edge.length = sqrt((n1.x - n2.x)**2 + (n1.z - n2.z)**2)
def number_of_nodes(self) -> int:
return len(self.nodes)
@ -104,7 +114,7 @@ class Graph:
f.write(f"n {node.x} {node.z} {node.name}\n")
f.write("\n")
for edge in self.edges:
f.write(f"e {edge.start} {edge.end} {edge.length}\n")
f.write(f"e {edge.start} {edge.end}\n")
@staticmethod
def load(path: str) -> Graph:
@ -121,7 +131,9 @@ class Graph:
x, z = int(x), int(z)
graph.add_node(x, z, name)
elif entry_type == "e":
start, end, length = values.split(" ", 2)
start, end, length = int(start), int(end), float(length)
graph.add_edge(start, end, length)
start, end = values.split(" ", 2)
start, end = int(start), int(end)
graph.add_edge(start, end, False)
graph.recompute_lengths()
return graph