class Rectangle(Polygon):
def __init__(self, x: float, y: float, width: float, height: float):
vertices = [
(x, y),
(x + width, y),
(x + width, y + height),
(x, y + height),
(x, y),
]
super().__init__(vertices)
self.width = width
self.height = height
@classmethod
def from_opposite_corners(
cls,
point_a: Tuple[float, float],
point_b: Tuple[float, float],
) -> "Rectangle":
x1, y1 = point_a
x2, y2 = point_b
min_x = min(x1, x2)
min_y = min(y1, y2)
width = abs(x2 - x1)
height = abs(y2 - y1)
return cls(min_x, min_y, width, height)
def area(self) -> float:
"""Vrací obsah obdélníku."""
return self.width * self.height
def perimeter(self) -> float:
return 2 * (self.width + self.height)
def __repr__(self) -> str:
return f"Obdélník {self.width}x{self.height}, hranice: {self.bounds}"