""" A class for triangles. >>> Triangle(Point(22, 1), Point(4, 12), Point(6, 3)).print() (22, 1), (4, 12), (6, 3) >>> Triangle(Point(0, 0), Point(0, 3), Point(4, 0)).perimeter() 12.0 """ from typing import Final from point import Point from polygon import Polygon class Triangle(Polygon): """The class for triangles.""" def __init__(self, p1: Point, p2: Point, p3: Point) -> None: """ Create a triangle. :param p1: the first point spanning the triangle :param p2: the second point spanning the triangle :param p3: the third point spanning the triangle """ if (p1.distance(p2) <= 0) or (p2.distance(p3) <= 0) or ( p3.distance(p1) <= 0): # check for non-emptiness raise ValueError("empty triangle") #: the first point spanning the triangle self.p1: Final[Point] = p1 #: the second point spanning the triangle self.p2: Final[Point] = p2 #: the third point spanning the triangle self.p3: Final[Point] = p3 def area(self) -> int | float: """ Get the area of this triangle. :return: the area of this triangle >>> Triangle(Point(-1, 2), Point(2, 3), Point(4, -3)).area() 10.0 """ return 0.5 * abs(self.p1.x * (self.p2.y - self.p3.y) + self.p2.x * (self.p3.y - self.p1.y) + self.p3.x * (self.p1.y - self.p2.y)) def points(self) -> tuple[Point, Point, Point]: """ Get the three points describing this triangle. :return: a tuple with the three corners of this triangle """ return self.p1, self.p2, self.p3