""" A class for rectangles. >>> Rectangle(Point(22, 1), Point(4, 12)).print() (4, 1), (4, 12), (22, 12), (22, 1) """ from typing import Final from point import Point from polygon import Polygon class Rectangle(Polygon): """A rectangle defined by its bottom-left and top-right corners.""" def __init__(self, p1: Point, p2: Point) -> None: """ Create a rectangle. :param p1: the first point spanning the rectangle :param p2: the second point spanning the rectangle """ if (p1.x == p2.x) or (p1.y == p2.y): # check for non-emptiness raise ValueError(f"{p1.x},{p1.y},{p2.x},{p2.y} is empty.") #: the bottom-left point spanning the rectangle self.p1: Final[Point] = Point(min(p1.x, p2.x), min(p1.y, p2.y)) #: the top-right point spanning the rectangle self.p2: Final[Point] = Point(max(p1.x, p2.x), max(p1.y, p2.y)) def area(self) -> int | float: """ Get the area of this rectangle. :return: the area of this rectangle >>> Rectangle(Point(7, 3), Point(12, 6)).area() 15 """ return (self.p2.x - self.p1.x) * (self.p2.y - self.p1.y) def perimeter(self) -> int | float: """ Get the perimeter of this rectangle. :return: the perimeter of this rectangle >>> Rectangle(Point(10, 5), Point(4, 9)).perimeter() 20 """ return 2 * ((self.p2.x - self.p1.x) + (self.p2.y - self.p1.y)) def points(self) -> tuple[Point, Point, Point, Point]: """ Get the four corner points of this rectangle. :return: a tuple with the four corners of this rectangle """ return (self.p1, Point(self.p1.x, self.p2.y), self.p2, Point(self.p2.x, self.p1.y))