"""A polygon is a figure described by its corner points."""
from typing import Iterable
from kahan_sum import KahanSum
from point import Point
from shape import Shape
class Polygon(Shape):
"""Polygons are shapes delimited by straight lines."""
def points(self) -> Iterable[Point]:
"""
Get a :class:`Iterable` over the points describing this polygon.
:return: the points describing the polygon
:raises NotImplementedError: Must be implemented by subclasses.
"""
raise NotImplementedError # must be implemented by subclasses
def perimeter(self) -> int | float:
"""
Get the perimeter of this polygon.
:return: the perimeter of this polygon
"""
previous: Point | None = None # the previous point
first: Point | None = None # the first point
total: KahanSum = KahanSum() # the total perimeter length sum
for current in self.points(): # Iterate over the points.
if previous is None: # We got the first point.
previous = first = current # Remember it for last step.
else: # We now have previous != None, so we can add length.
total.add(previous.distance(current)) # Add length.
previous = current # Current point becomes previous.
total.add(previous.distance(first)) # distance back to start
return total.result() # Return the perimeter.
def print(self) -> None:
"""Print the points of this polygon."""
print(", ".join(f"({p.x}, {p.y})" for p in self.points()))