From 452f6ad0413244d548ff62b2bc84b233c58bb1c0 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 17 Apr 2024 17:47:39 +0200 Subject: [PATCH] fixed stretching in MapDisplay --- .idea/vcs.xml | 7 +++++++ map_display.py | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..2a4b436 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,5 +1,12 @@ + + + + + + diff --git a/map_display.py b/map_display.py index a3bf7cd..1b00cec 100644 --- a/map_display.py +++ b/map_display.py @@ -26,14 +26,26 @@ class MapDisplay(Display): self.max_lon: float = max_lon self.min_lat: float = min_lat self.max_lat: float = max_lat + self._precomputeDisplayValues() self.cities: list[tuple[Vec2, str, str]] = cities - def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]: - x = (lon - self.min_lon) / (self.max_lon - self.min_lon) - y = (lat - self.min_lat) / (self.max_lat - self.min_lat) + def _precomputeDisplayValues(self) -> None: + width, height = self.surf.get_size() + self._lon_span = self.max_lon - self.min_lon + self._lat_span = self.max_lat - self.min_lat + r1 = width / self._lon_span + r2 = height / self._lat_span - w, h = self.surf.get_size() - return x * w, h - y * h + self._r = min(r1, r2) + + self._ox = (width - self._lon_span * self._r) / 2 + self._oy = (height - self._lat_span * self._r) / 2 + + def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]: + x = (lon - self.min_lon) * self._r + self._ox + y = (lat - self.min_lat) * self._r + self._oy + + return x, self.surf.get_height() - y def draw_path(self, path: Path) -> None: self.draw_colored_path(path, None)