Add mid coordinate and midpoint properties to Rect
This commit is contained in:
parent
52a71b3b8a
commit
e7a5af59ed
1 changed files with 18 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# Eryn Wells <eryn@erynwells.me>
|
# Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
class Point:
|
class Point:
|
||||||
__slots__ = ('x', 'y')
|
__slots__ = ('x', 'y')
|
||||||
|
|
||||||
|
@ -34,6 +36,10 @@ class Size:
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
|
|
||||||
|
@property
|
||||||
|
def as_tuple(self) -> Tuple[int, int]:
|
||||||
|
return (self.width, self.height)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(w:{self.width}, h:{self.height})'
|
return f'(w:{self.width}, h:{self.height})'
|
||||||
|
|
||||||
|
@ -58,6 +64,14 @@ class Rect:
|
||||||
def min_y(self) -> int:
|
def min_y(self) -> int:
|
||||||
return self.origin.y
|
return self.origin.y
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mid_x(self) -> int:
|
||||||
|
return int(self.origin.x + self.size.width / 2)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mid_y(self) -> int:
|
||||||
|
return int(self.origin.y + self.size.height / 2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max_x(self) -> int:
|
def max_x(self) -> int:
|
||||||
return self.origin.x + self.size.width - 1
|
return self.origin.x + self.size.width - 1
|
||||||
|
@ -66,6 +80,10 @@ class Rect:
|
||||||
def max_y(self) -> int:
|
def max_y(self) -> int:
|
||||||
return self.origin.y + self.size.height - 1
|
return self.origin.y + self.size.height - 1
|
||||||
|
|
||||||
|
@property
|
||||||
|
def midpoint(self) -> Point:
|
||||||
|
return Point(self.mid_x, self.mid_y)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'({self.origin}, {self.size})'
|
return f'({self.origin}, {self.size})'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue