Issue #84: add primitive road connections.

This commit is contained in:
Sergey Vartanov 2021-09-18 20:25:51 +03:00
parent b46e65f2ec
commit aa82353f00
6 changed files with 258 additions and 150 deletions

View file

@ -6,6 +6,8 @@ import numpy as np
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
from shapely.geometry import LineString
def compute_angle(vector: np.ndarray) -> float:
"""
@ -38,6 +40,37 @@ def norm(vector: np.ndarray) -> np.ndarray:
return vector / np.linalg.norm(vector)
class Polyline:
"""
List of connected points.
"""
def __init__(self, points: list[np.ndarray]) -> None:
self.points: list[np.ndarray] = points
def get_path(self, parallel_offset: float = 0) -> str:
"""Construct SVG path commands."""
points: list[np.ndarray]
try:
points = (
LineString(self.points).parallel_offset(parallel_offset).coords
if parallel_offset
else self.points
)
except ValueError:
points = self.points
path: str = "M " + " L ".join(f"{x[0]},{x[1]}" for x in points)
return path + (" Z" if np.allclose(points[0], points[-1]) else "")
def shorten(self, index: int) -> None:
"""Make shorten part specified with index."""
index_2: int = 1 if index == 0 else -2
diff: np.ndarray = self.points[index_2] - self.points[index]
self.points[index] = (
self.points[index] + diff / np.linalg.norm(diff) * 5
)
class Line:
"""Infinity line: Ax + By + C = 0."""