train-journey-visuals/map_display.py

38 lines
1.2 KiB
Python
Raw Normal View History

2024-04-15 19:18:52 +00:00
from typing import Optional
import pygame
2024-04-15 20:27:00 +00:00
from path import Path
2024-04-15 19:18:52 +00:00
class MapDisplay:
PATH_WIDTH = 5
def __init__(self, surf: pygame.Surface, min_lat: float, max_lat: float, min_lon: float, max_lon: float):
self.surf: pygame.Surface = surf
self.min_lat: float = min_lat
self.max_lat: float = max_lat
self.min_lon: float = min_lon
self.max_lon: float = max_lon
2024-04-15 20:27:00 +00:00
def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]:
2024-04-15 19:18:52 +00:00
x = (lon - self.min_lon) / (self.max_lon - self.min_lon)
y = (lat - self.min_lat) / (self.max_lat - self.min_lat)
w, h = self.surf.get_size()
return x * w, y * h
2024-04-15 20:27:00 +00:00
def draw_path(self, path: Path) -> None:
self.draw_colored_path(path, None)
def draw_colored_path(self, path: Path, colors: Optional[list[tuple[int, int, int]]] = None) -> None:
for i, pt in enumerate(path.points):
lon = pt.x
lat = pt.y
x, y = self.real_to_screen(lon, lat)
2024-04-15 19:18:52 +00:00
col = (255, 255, 255) if colors is None else colors[i]
pygame.draw.circle(self.surf, col, (x, y), self.PATH_WIDTH)
2024-04-15 20:27:00 +00:00
def draw_segment(self, path: Path, start_i: int, end_i: int) -> None:
2024-04-15 19:18:52 +00:00
raise NotImplementedError