train-journey-visuals/path.py

29 lines
805 B
Python
Raw Permalink Normal View History

2024-04-15 19:18:52 +00:00
from vec import Vec2
class Path:
def __init__(self, points: list[Vec2], extra_data: list):
self.points: list[Vec2] = points
self.extra_data = extra_data
2024-04-15 19:21:24 +00:00
self.normals: list[Vec2] = []
self._init_normals()
2024-04-15 19:18:52 +00:00
2024-04-15 19:21:24 +00:00
def _init_normals(self) -> None:
2024-04-15 21:14:18 +00:00
pt0 = self.points[0]
self.normals.append(Vec2(pt0.y, -pt0.x))
2024-04-15 19:18:52 +00:00
for i in range(1, len(self.points) - 1):
pt1 = self.points[i-1]
pt2 = self.points[i]
pt3 = self.points[i+1]
2024-04-15 19:21:24 +00:00
d1 = (pt1 - pt2).normalized()
d2 = (pt3 - pt2).normalized()
2024-04-15 19:18:52 +00:00
2024-04-15 19:21:24 +00:00
d = (d1 + d2).normalized()
2024-04-15 19:18:52 +00:00
if d2.cross(d) < 0:
d = -d
2024-04-15 19:21:24 +00:00
self.normals.append(d)
2024-04-15 21:14:18 +00:00
ptl = self.points[-1]
self.normals.append(Vec2(ptl.y, -ptl.x))