boxespy/boxes/extents.py

44 lines
1.3 KiB
Python
Raw Normal View History

class Extents:
__slots__ = "xmin ymin xmax ymax".split()
2023-02-07 15:26:58 +01:00
def __init__(self, xmin: float = float('inf'), ymin: float = float('inf'), xmax: float = float('-inf'), ymax: float = float('-inf')) -> None:
self.xmin = xmin
self.ymin = ymin
self.xmax = xmax
self.ymax = ymax
2023-02-07 15:26:58 +01:00
def add(self, x: float, y: float) -> None:
self.xmin = min(self.xmin, x)
self.xmax = max(self.xmax, x)
self.ymin = min(self.ymin, y)
self.ymax = max(self.ymax, y)
2023-02-07 15:26:58 +01:00
def extend(self, l) -> None:
for x, y in l:
self.add(x, y)
2023-02-07 15:26:58 +01:00
def __add__(self, extent):
# todo: why can this happen?
if extent == 0:
2023-02-07 15:26:58 +01:00
return Extents(self.xmin, self.ymin, self.xmax, self.ymax)
return Extents(
2023-02-07 15:26:58 +01:00
min(self.xmin, extent.xmin), min(self.ymin, extent.ymin),
max(self.xmax, extent.xmax), max(self.ymax, extent.ymax)
)
2023-02-07 15:26:58 +01:00
def __radd__(self, extent):
if extent == 0:
2023-02-07 15:26:58 +01:00
return Extents(self.xmin, self.ymin, self.xmax, self.ymax)
return self.__add__(extent)
2023-01-23 00:46:17 +01:00
@property
def width(self) -> float:
return self.xmax - self.xmin
2023-01-23 00:46:17 +01:00
@property
def height(self) -> float:
return self.ymax - self.ymin
2023-01-08 19:41:02 +01:00
def __repr__(self) -> str:
return f'Extents ({self.xmin},{self.ymin})-({self.xmax},{self.ymax})'