-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.py
97 lines (75 loc) · 2.32 KB
/
shapes.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from functools import total_ordering
@total_ordering
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
@property
def x(self):
return self._x
@x.setter
def x(self, other):
assert isinstance(other, (int, float, complex))
self._x = other
@property
def y(self):
return self._y
@y.setter
def y(self, other):
assert isinstance(other, (int, float, complex))
self._y = other
@property
def tuple(self):
return (self.x, self.y)
@tuple.setter
def tuple(self, other):
self.x, self.y = other
def __lt__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x < other.x and self.y < other.y
def __le__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x <= other.x and self.y <= other.y
def __eq__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.tuple == other.tuple
def __ne__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x != other.x or self.y != other.y
def __gt__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x > other.x and self.y > other.y
def __ge__(self, other):
if not isinstance(other, Point):
return NotImplemented
return self.x >= other.x and self.y >= other.y
def __str__(self):
return f"{type(self)} ({self.x}, {self.y})"
class Rectangle:
def __init__(self, point1, point2):
assert isinstance(point1, Point)
assert isinstance(point2, Point)
self.point1 = point1
self.point2 = point2
@property
def point1(self):
return self._point1
@point1.setter
def point1(self, new_point):
assert isinstance(new_point, Point)
self._point1 = new_point
@property
def point2(self):
return self._point2
@point2.setter
def point2(self, new_point):
assert isinstance(new_point, Point)
self._point2 = new_point
def contains_point(self, point):
assert isinstance(point, Point)
return point >= self.point1 and point <= self.point2