Compare commits

..

No commits in common. "509c7fec8afdbef08296cd0dcc71053e2c88883e" and "ff2c184c65b7eca055cc048425c3be7b4f80be10" have entirely different histories.

2 changed files with 68 additions and 83 deletions

View File

@ -29,8 +29,7 @@ class Editor:
self.running: bool = False self.running: bool = False
self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE) self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE)
self.clock: pygame.time.Clock = pygame.time.Clock() self.clock: pygame.time.Clock = pygame.time.Clock()
self.left_drag_pos: Optional[tuple[int, int]] = None self.drag_pos: Optional[tuple[int, int]] = None
self.mid_drag_pos: Optional[tuple[int, int]] = None
self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20) self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20)
self.loading_font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 30) self.loading_font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 30)
self.zooms_texts: list[pygame.Surface] = list(map( self.zooms_texts: list[pygame.Surface] = list(map(
@ -55,7 +54,6 @@ class Editor:
def process_events(self) -> None: def process_events(self) -> None:
events = pygame.event.get() events = pygame.event.get()
keys = pygame.key.get_pressed()
for event in events: for event in events:
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self.state = State.STOPPING self.state = State.STOPPING
@ -71,19 +69,12 @@ class Editor:
self.zoom_out() self.zoom_out()
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 2: if event.button == 2:
self.mid_drag_pos = event.pos self.drag_pos = event.pos
elif event.button == 1 and keys[pygame.K_LCTRL]:
self.left_drag_pos = event.pos
elif event.button == 4:
self.zoom_in()
elif event.button == 5:
self.zoom_out()
elif event.type == pygame.MOUSEBUTTONUP: elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 2: if event.button == 2:
self.mid_drag_pos = None self.drag_pos = None
elif event.button == 1:
self.left_drag_pos = None
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
self.center[0] -= 4 / self.zoom self.center[0] -= 4 / self.zoom
if keys[pygame.K_RIGHT]: if keys[pygame.K_RIGHT]:
@ -95,16 +86,12 @@ class Editor:
mbtns = pygame.mouse.get_pressed() mbtns = pygame.mouse.get_pressed()
mpos = pygame.mouse.get_pos() mpos = pygame.mouse.get_pos()
if mbtns[1] or (mbtns[0] and keys[pygame.K_LCTRL]): if mbtns[1]:
drag_pos = self.mid_drag_pos if mbtns[1] else self.left_drag_pos dx = mpos[0] - self.drag_pos[0]
dx = mpos[0] - drag_pos[0] dy = mpos[1] - self.drag_pos[1]
dy = mpos[1] - drag_pos[1]
self.center[0] -= dx / self.zoom self.center[0] -= dx / self.zoom
self.center[1] -= dy / self.zoom self.center[1] -= dy / self.zoom
if mbtns[0] and keys[pygame.K_LCTRL]: self.drag_pos = mpos
self.left_drag_pos = mpos
if mbtns[1]:
self.mid_drag_pos = mpos
def render(self) -> None: def render(self) -> None:
self.win.fill((0, 0, 0)) self.win.fill((0, 0, 0))

View File

@ -1,88 +1,86 @@
from math import inf from math import inf
from typing import Iterator, Optional
class Node:
def __init__(self, x: int, y: int):
self.x: int = x
self.y: int = y
class Edge: class Edge:
def __init__(self, start: int, end: int, length: float):
self.length: float = length
self.start: int = start
self.end: int = end
length = 0
def __init__(self, start, end, length):
self.length = length
self.start = start
self.end = end
class Node:
def __init__(self, xpos, ypos):
self.xpos = xpos
self.ypos = ypos
class Graph: class Graph:
def __init__(self): def __init__(self):
self.edges: list[Edge] = [] self.edges = list()
self.nodes: list[Node] = [] self.nodes = list()
def add_node(self, x: int, y: int) -> None: def add_node(self, xpos, ypos):
self.nodes.append(Node(x, y)) self.nodes.append(Node(xpos, ypos))
def add_edge(self, start_index: int, end_index: int, length: float) -> None: def add_edge(self, start_index, end_index, length):
self.edges.append(Edge(start_index, end_index, length)) self.edges.append(Edge(start_index, end_index, length))
def edges_adjacent_to(self, node_i: int) -> Iterator[Edge]:
return filter(lambda e: e.start == node_i or e.end == node_i, self.edges)
def dijkstra(self, source_index: int, target_index: int) -> Optional[list[int]]: def Dijkstra(graph, source_index, target_index):
n = len(self.nodes)
if source_index < 0 or source_index >= n: n = len(graph.nodes)
return None
if target_index < 0 or target_index >= n: if (target_index >= n):
return None return None
unvisited = list(range(n)) unvisited = list(range(n))
distances_from_start = [inf] * n distances_from_start = [inf] * n
distances_from_start[source_index] = 0 distances_from_start[source_index] = 0
node_sequences = [[] for _ in range(n)] node_sequences = [list() for i in range(n)]
node_sequences[source_index] = [source_index] node_sequences[source_index] = [source_index]
while True: while(True):
current_index = min(unvisited, key=lambda i: distances_from_start[i]) try:
current_index = min(unvisited, key = lambda i: distances_from_start[i])
except ValueError:
break
if current_index == target_index: if current_index == target_index:
break break
unvisited.remove(current_index) unvisited.remove(current_index)
for edge in self.edges_adjacent_to(current_index): for edge in filter(
start = current_index lambda e: e.start == current_index or e.end == current_index,
end = edge.end if edge.start == current_index else edge.start graph.edges):
if end in unvisited and distances_from_start[end] > distances_from_start[start] + edge.length: start = current_index
distances_from_start[end] = distances_from_start[start] + edge.length end = edge.end if edge.start == current_index else edge.start
node_sequences[end] = node_sequences[start].copy()
node_sequences[end].append(end)
return node_sequences[target_index] if end in unvisited and distances_from_start[end] > distances_from_start[start] + edge.length:
distances_from_start[end] = distances_from_start[start] + edge.length
node_sequences[end] = node_sequences[start].copy()
node_sequences[end].append(end)
return node_sequences[target_index]
def main() -> None: graph = Graph()
graph = Graph()
graph.add_node(1, 2) graph.add_node(1, 2)
graph.add_node(4, 7) graph.add_node(4, 7)
graph.add_node(3, 1) graph.add_node(3,1)
graph.add_node(-2, 0) graph.add_node(-2,0)
graph.add_node(0, 0) graph.add_node(0,0)
graph.add_edge(0, 1, 1) graph.add_edge(0, 1, 1)
graph.add_edge(1, 2, 2) graph.add_edge(1, 2, 2)
graph.add_edge(2, 3, 3) graph.add_edge(2, 3, 3)
graph.add_edge(3, 0, 1) graph.add_edge(3, 0, 1)
graph.add_edge(1, 3, 3) graph.add_edge(1, 3, 3)
print(graph.dijkstra(0, 3)) print(Dijkstra(graph, 0, 5))
if __name__ == "__main__":
main()