-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle.py
More file actions
68 lines (55 loc) · 1.83 KB
/
circle.py
File metadata and controls
68 lines (55 loc) · 1.83 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
from shape import Shape
from utils import validate_number
import math
class Circle(Shape):
"""
Represents a circle shape inheriting from Shape.
Attributes:
x (float): X-coordinate of the circle center.
y (float): Y-coordinate of the circle center.
radius (float): Circle radius (must be positive).
"""
def __init__(self, x: float = 0, y: float = 0, radius: float = 1):
super().__init__(x, y)
self.radius = radius
@property
def radius(self) -> float:
return self._radius
@radius.setter
def radius(self, value: float):
validate_number(value)
if value < 0:
raise ValueError("The radius cannot be negative")
self._radius = float(value)
@property
def area(self) -> float:
"""Return the area of the circle (πr²)."""
return math.pi * self.radius**2
@property
def perimeter(self) -> float:
"""Return the circumference of the circle (2πr)."""
return 2 * math.pi * self.radius
@property
def is_unit_circle(self) -> bool:
"""
Check if the circle is a unit circle. This is taken from 'LLM'.
A unit circle has radius = 1 and center at (0, 0).
"""
return (
math.isclose(self.radius, 1)
and math.isclose(self.x, 0)
and math.isclose(self.y, 0)
)
def __repr__(self) -> str:
return f"Circle(x={self.x}, y={self.y}, radius={self.radius})"
def __str__(self) -> str:
return (
f"Circle at ({self.x}, {self.y}) with radius {self.radius}. "
f"Area: {self.area:.2f}, Perimeter: {self.perimeter:.2f}"
)
if __name__ == "__main__":
c = Circle(radius=5)
print(c)
print("Is unit circle?", c.is_unit_circle)
c.translate(3, -1)
print("Moved to:", (c.x, c.y))