Add a couple geometry methods

- Vector.from_point to convert a point to a vector
- Rect.width and Rect.height convenience properties
This commit is contained in:
Eryn Wells 2023-02-12 15:52:26 -08:00
parent b0b75f7e76
commit ec28f984da

View file

@ -71,6 +71,11 @@ class Vector:
dx: int = 0
dy: int = 0
@classmethod
def from_point(cls, point: Point) -> 'Vector':
'''Create a Vector from a Point'''
return Vector(point.x, point.y)
def __iter__(self):
yield self.dx
yield self.dy
@ -164,6 +169,16 @@ class Rect:
'''Maximum y-value that is still within the bounds of this rectangle.'''
return self.origin.y + self.size.height - 1
@property
def width(self) -> int:
'''The width of the rectangle. A convenience property for accessing `self.size.width`.'''
return self.size.width
@property
def height(self) -> int:
'''The height of the rectangle. A convenience property for accessing `self.size.height`.'''
return self.size.height
@property
def midpoint(self) -> Point:
'''A Point in the middle of the Rect'''