-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrect.py
More file actions
45 lines (37 loc) · 1.22 KB
/
rect.py
File metadata and controls
45 lines (37 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Self
class Rect:
def __init__(
self, *, left: float = 0, top: float = 0, right: float = 0, bottom: float = 0
):
self.left: float = left
self.top: float = top
self.right: float = right
self.bottom: float = bottom
@property
def width(self) -> float:
return self.right - self.left
@property
def height(self) -> float:
return self.bottom - self.top
def as_tuple(self) -> tuple[float, float, float, float]:
return self.left, self.top, self.right, self.bottom
def add_point(self, x: float, y: float):
if x < self.left:
self.left = x
if x > self.right:
self.right = x
if y < self.top:
self.top = y
if y > self.bottom:
self.bottom = y
def merge_bounds(self, other: Self):
if other.left < self.left:
self.left = other.left
if other.right > self.right:
self.right = other.right
if other.top < self.top:
self.top = other.top
if other.bottom > self.bottom:
self.bottom = other.bottom
def __str__(self):
return f"Rect{self.left, self.top, self.right, self.bottom}"