2022-04-30 21:59:33 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2022-05-01 09:26:20 -07:00
|
|
|
from typing import Tuple, overload
|
2022-05-01 00:08:10 -07:00
|
|
|
|
2022-04-30 21:59:33 -07:00
|
|
|
class Point:
|
|
|
|
__slots__ = ('x', 'y')
|
|
|
|
|
|
|
|
def __init__(self, x: int = 0, y: int = 0):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
2022-05-01 09:26:20 -07:00
|
|
|
@overload
|
2022-05-01 09:51:22 -07:00
|
|
|
def __add__(self, other: 'Vector') -> 'Point':
|
2022-05-01 09:26:20 -07:00
|
|
|
...
|
|
|
|
|
2022-05-01 09:51:22 -07:00
|
|
|
def __add__(self, other) -> 'Point':
|
2022-05-01 09:26:20 -07:00
|
|
|
if not isinstance(other, Vector):
|
|
|
|
raise TypeError('Only Vector can be added to a Point')
|
|
|
|
return Point(self.x + other.dx, self.y + other.dy)
|
|
|
|
|
2022-05-01 09:51:22 -07:00
|
|
|
@overload
|
|
|
|
def __eq__(self, other: 'Point') -> bool:
|
|
|
|
...
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Point):
|
|
|
|
raise TypeError('Points can only be compared to other Points')
|
|
|
|
return self.x == other.x and self.y == other.y
|
|
|
|
|
2022-04-30 21:59:33 -07:00
|
|
|
def __str__(self):
|
|
|
|
return f'(x:{self.x}, y:{self.y})'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'Point({self.x}, {self.y})'
|
|
|
|
|
|
|
|
class Vector:
|
|
|
|
__slots__ = ('dx', 'dy')
|
|
|
|
|
|
|
|
def __init__(self, dx: int = 0, dy: int = 0):
|
|
|
|
self.dx = dx
|
|
|
|
self.dy = dy
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'(δx:{self.x}, δy:{self.y})'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'Vector({self.x}, {self.y})'
|
|
|
|
|
|
|
|
class Size:
|
|
|
|
__slots__ = ('width', 'height')
|
|
|
|
|
|
|
|
def __init__(self, width: int = 0, height: int = 0):
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
|
2022-05-01 00:08:10 -07:00
|
|
|
@property
|
|
|
|
def as_tuple(self) -> Tuple[int, int]:
|
|
|
|
return (self.width, self.height)
|
|
|
|
|
2022-04-30 21:59:33 -07:00
|
|
|
def __str__(self):
|
|
|
|
return f'(w:{self.width}, h:{self.height})'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'Size({self.width}, {self.height})'
|
|
|
|
|
|
|
|
class Rect:
|
|
|
|
__slots__ = ('origin', 'size')
|
|
|
|
|
|
|
|
def __init__(self, x: int = 0, y: int = 0, w: int = 0, h: int = 0):
|
|
|
|
'''
|
|
|
|
Construct a new rectangle.
|
|
|
|
'''
|
|
|
|
self.origin = Point(x, y)
|
|
|
|
self.size = Size(w, h)
|
|
|
|
|
2022-04-30 23:29:24 -07:00
|
|
|
@property
|
|
|
|
def min_x(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''Minimum x-value that is still within the bounds of this rectangle. This is the origin's x-value.'''
|
2022-04-30 23:29:24 -07:00
|
|
|
return self.origin.x
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_y(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''Minimum y-value that is still within the bounds of this rectangle. This is the origin's y-value.'''
|
2022-04-30 23:29:24 -07:00
|
|
|
return self.origin.y
|
|
|
|
|
2022-05-01 00:08:10 -07:00
|
|
|
@property
|
|
|
|
def mid_x(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''The x-value of the center point of this rectangle.'''
|
2022-05-01 00:08:10 -07:00
|
|
|
return int(self.origin.x + self.size.width / 2)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mid_y(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''The y-value of the center point of this rectangle.'''
|
2022-05-01 00:08:10 -07:00
|
|
|
return int(self.origin.y + self.size.height / 2)
|
|
|
|
|
2022-04-30 21:59:33 -07:00
|
|
|
@property
|
|
|
|
def max_x(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''Maximum x-value that is still within the bounds of this rectangle.'''
|
2022-04-30 23:29:24 -07:00
|
|
|
return self.origin.x + self.size.width - 1
|
2022-04-30 21:59:33 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_y(self) -> int:
|
2022-05-01 09:26:20 -07:00
|
|
|
'''Maximum y-value that is still within the bounds of this rectangle.'''
|
2022-04-30 23:29:24 -07:00
|
|
|
return self.origin.y + self.size.height - 1
|
2022-04-30 21:59:33 -07:00
|
|
|
|
2022-05-01 00:08:10 -07:00
|
|
|
@property
|
|
|
|
def midpoint(self) -> Point:
|
|
|
|
return Point(self.mid_x, self.mid_y)
|
|
|
|
|
2022-04-30 21:59:33 -07:00
|
|
|
def __str__(self):
|
2022-05-01 09:26:20 -07:00
|
|
|
return f'[{self.origin}, {self.size}]'
|
2022-04-30 21:59:33 -07:00
|
|
|
|
|
|
|
def __repr__(self):
|
2022-05-01 09:26:20 -07:00
|
|
|
return f'{self.__class__.__name__}({self.origin.x}, {self.origin.y}, {self.size.width}, {self.size.height})'
|