eniallator

Python Path Creation

Mar 12th, 2026
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. from typing import List, NamedTuple, Optional
  2.  
  3.  
  4. type OptFloatSlice = slice[Optional[float], float, Optional[float]]
  5.  
  6.  
  7. def float_range(slc: OptFloatSlice) -> List[float]:
  8.     start = slc.start or 0.0
  9.     step = slc.step or 1.0
  10.     return [i * step + start for i in range(int(slc.stop - start / step))]
  11.  
  12.  
  13. class Spatial[T](NamedTuple):
  14.     x: T
  15.     y: T
  16.     z: T
  17.  
  18.  
  19. class Vector3(Spatial[float]):
  20.     pass
  21.  
  22.  
  23. class Bounds(Spatial[float | OptFloatSlice]):
  24.     pass
  25.  
  26.  
  27. class Ranges(Spatial[List[float]]):
  28.     @staticmethod
  29.     def from_bounds(bounds: Bounds) -> Ranges:
  30.         return Ranges(
  31.             float_range(bounds.x) if isinstance(bounds.x, slice) else [bounds.x],
  32.             float_range(bounds.y) if isinstance(bounds.y, slice) else [bounds.y],
  33.             float_range(bounds.z) if isinstance(bounds.z, slice) else [bounds.z],
  34.         )
  35.  
  36.     def to_path(self) -> List[Vector3]:
  37.         return [
  38.             Vector3(x, y, z)
  39.             for x in self.x
  40.             for y in self.y
  41.             for z in self.z
  42.         ]
  43.  
  44.  
  45. bounds = Bounds(10.0, slice(7.0), 15.0)
  46.  
  47. ranges = Ranges.from_bounds(bounds)
  48.  
  49. print(ranges.to_path())
Advertisement
Add Comment
Please, Sign In to add comment