implemented segments
This commit is contained in:
parent
68a9d853c5
commit
3784973647
@ -3,24 +3,32 @@ from typing import Optional
|
||||
import pygame
|
||||
|
||||
from path import Path
|
||||
from vec import Vec2
|
||||
|
||||
|
||||
class MapDisplay:
|
||||
PATH_WIDTH = 5
|
||||
SEGMENT_SIZE = 20
|
||||
MIN_SEGMENT_LENGTH = 5
|
||||
|
||||
def __init__(self, surf: pygame.Surface, min_lat: float, max_lat: float, min_lon: float, max_lon: float):
|
||||
def __init__(self,
|
||||
surf: pygame.Surface,
|
||||
min_lon: float,
|
||||
max_lon: float,
|
||||
min_lat: float,
|
||||
max_lat: 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
|
||||
self.min_lat: float = min_lat
|
||||
self.max_lat: float = max_lat
|
||||
|
||||
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)
|
||||
|
||||
w, h = self.surf.get_size()
|
||||
return x * w, y * h
|
||||
return x * w, h - y * h
|
||||
|
||||
def draw_path(self, path: Path) -> None:
|
||||
self.draw_colored_path(path, None)
|
||||
@ -34,4 +42,18 @@ class MapDisplay:
|
||||
pygame.draw.circle(self.surf, col, (x, y), self.PATH_WIDTH)
|
||||
|
||||
def draw_segment(self, path: Path, start_i: int, end_i: int) -> None:
|
||||
raise NotImplementedError
|
||||
if end_i - start_i < self.MIN_SEGMENT_LENGTH:
|
||||
return
|
||||
|
||||
points = []
|
||||
for i in range(start_i, end_i):
|
||||
pt = path.points[i]
|
||||
pt = Vec2(*self.real_to_screen(pt.x, pt.y))
|
||||
n = path.normals[i]
|
||||
n = Vec2(n.x, -n.y)
|
||||
pt1 = pt + n * self.SEGMENT_SIZE
|
||||
pt2 = pt - n * self.SEGMENT_SIZE
|
||||
points.insert(0, (pt1.x, pt1.y))
|
||||
points.append((pt2.x, pt2.y))
|
||||
|
||||
pygame.draw.lines(self.surf, (255, 255, 255), True, points)
|
||||
|
5
path.py
5
path.py
@ -9,6 +9,8 @@ class Path:
|
||||
self._init_normals()
|
||||
|
||||
def _init_normals(self) -> None:
|
||||
pt0 = self.points[0]
|
||||
self.normals.append(Vec2(pt0.y, -pt0.x))
|
||||
for i in range(1, len(self.points) - 1):
|
||||
pt1 = self.points[i-1]
|
||||
pt2 = self.points[i]
|
||||
@ -22,3 +24,6 @@ class Path:
|
||||
d = -d
|
||||
|
||||
self.normals.append(d)
|
||||
|
||||
ptl = self.points[-1]
|
||||
self.normals.append(Vec2(ptl.y, -ptl.x))
|
@ -2,25 +2,25 @@ import pygame
|
||||
|
||||
from map_display import MapDisplay
|
||||
from path import Path
|
||||
from vec import Vec2
|
||||
|
||||
|
||||
class SpeedMapDisplay(MapDisplay):
|
||||
def __init__(self,
|
||||
surf: pygame.Surface,
|
||||
min_lat: float,
|
||||
max_lat: float,
|
||||
min_lon: float,
|
||||
max_lon: float,
|
||||
min_lat: float,
|
||||
max_lat: float,
|
||||
min_speed_col: tuple[int, int, int],
|
||||
max_speed_col: tuple[int, int, int],
|
||||
segment_threshold: float):
|
||||
super().__init__(surf, min_lat, max_lat, min_lon, max_lon)
|
||||
super().__init__(surf, min_lon, max_lon, min_lat, max_lat)
|
||||
self.min_speed_col: tuple[int, int, int] = min_speed_col
|
||||
self.max_speed_col: tuple[int, int, int] = max_speed_col
|
||||
self.segment_threshold: float = segment_threshold
|
||||
|
||||
def draw_path(self, path: Path) -> None:
|
||||
colors = []
|
||||
min_speed = min(path.extra_data)
|
||||
max_speed = max(path.extra_data)
|
||||
|
||||
@ -28,6 +28,18 @@ class SpeedMapDisplay(MapDisplay):
|
||||
|
||||
self.draw_colored_path(path, colors)
|
||||
|
||||
in_segment = False
|
||||
start_i = 0
|
||||
for i, speed in enumerate(path.extra_data):
|
||||
if speed >= self.segment_threshold:
|
||||
if not in_segment:
|
||||
in_segment = True
|
||||
start_i = i
|
||||
|
||||
elif in_segment:
|
||||
in_segment = False
|
||||
self.draw_segment(path, start_i, i)
|
||||
|
||||
def interpolate_color(self, speed: float, min_speed: float, max_speed: float) -> tuple[int, int, int]:
|
||||
r_span = self.max_speed_col[0] - self.min_speed_col[0]
|
||||
g_span = self.max_speed_col[1] - self.min_speed_col[1]
|
||||
|
Loading…
Reference in New Issue
Block a user