-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes_example.py
More file actions
83 lines (59 loc) · 2.05 KB
/
shapes_example.py
File metadata and controls
83 lines (59 loc) · 2.05 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
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
import math
class Point:
__match_args__ = ("x", "y")
def __init__(self, xpos: int, ypos: int):
self.x: int | float = xpos
self.y: int | float = ypos
class Shape:
__match_args__ = ("point",)
def __init__(self, p: Point) -> None:
self.point = p
def area(self) -> int | float:
match self:
case Circle():
return math.pi * self.radius**2
case Shape(side=s):
return s**2
case Rectangle(_, w, h):
return w * h
case _:
raise Exception("Unhandled Shape!!")
def contains_point(self, point: Point) -> bool:
match self:
case Circle(Point(x, y), r):
return (point.x - x) ** 2 + (point.y - y) ** 2 <= r**2
case Square(p, s):
return Rectangle(p, s, s).contains_point(point)
case Rectangle(Point(x, y), w, h) if (
point.x >= x and point.x <= x + w and point.y >= y and point.y <= y + h
):
return True
case Rectangle():
return False
case _:
raise Exception("Unhandled Shape!")
class Circle(Shape):
__match_args__ = ("point", "radius")
def __init__(self, p: Point, r: int | float) -> None:
super().__init__(p)
self.radius: int | float = r
class Rectangle(Shape):
__match_args__ = ("point", "width", "height")
def __init__(self, p: Point, w: int | float, h: int | float) -> None:
super().__init__(p)
self.width: int | float = w
self.height: int | float = h
class Square(Rectangle):
__match_args__ = ("point", "side")
def __init__(
self, point: Point, s: int
):
super().__init__(point, s, s)
self.side = s
if __name__ == "__main__":
testc = Circle(Point(0, 0), 2)
testr = Rectangle(Point(1, 1), 2, 4)
tests = Square(Point(3, 5), 6)
testp = Point(1, 1)
for i in (testc, testr, tests):
print(f"{i.area()=} {i.contains_point(testp)=}")