geometry refinements

This commit is contained in:
Will McGugan
2022-01-02 16:20:11 +00:00
parent 9753b96ad6
commit 911d9024cc

View File

@@ -119,15 +119,19 @@ class Size(NamedTuple):
width, height = self width, height = self
return Region(0, 0, width, height) return Region(0, 0, width, height)
def __add__(self, other: tuple[int, int]) -> Size: def __add__(self, other: object) -> Size:
width, height = self if isinstance(other, tuple):
width2, height2 = other width, height = self
return Size(width + width2, height + height2) width2, height2 = other
return Size(width + width2, height + height2)
return NotImplemented
def __sub__(self, other: tuple[int, int]) -> Size: def __sub__(self, other: object) -> Size:
width, height = self if isinstance(other, tuple):
width2, height2 = other width, height = self
return Size(width - width2, height - height2) width2, height2 = other
return Size(width - width2, height - height2)
return NotImplemented
def contains(self, x: int, y: int) -> bool: def contains(self, x: int, y: int) -> bool:
"""Check if a point is in the size. """Check if a point is in the size.
@@ -274,14 +278,14 @@ class Region(NamedTuple):
"""A range object for Y coordinates""" """A range object for Y coordinates"""
return range(self.y, self.y + self.height) return range(self.y, self.y + self.height)
def __add__(self, other: Any) -> Region: def __add__(self, other: object) -> Region:
if isinstance(other, tuple): if isinstance(other, tuple):
ox, oy = other ox, oy = other
x, y, width, height = self x, y, width, height = self
return Region(x + ox, y + oy, width, height) return Region(x + ox, y + oy, width, height)
return NotImplemented return NotImplemented
def __sub__(self, other: Any) -> Region: def __sub__(self, other: object) -> Region:
if isinstance(other, tuple): if isinstance(other, tuple):
ox, oy = other ox, oy = other
x, y, width, height = self x, y, width, height = self