Polygon Perimeter and Area Calculator

Implement a Python class Polygon that represents a polygon with any number of sides (n). The class should have a method to calculate and return the perimeter, given the side length, and a method to calculate the area given the side length and apothem (distance from the center to any side). Note that the area of a polygon can be calculated using the formula 1/2 * apothem * perimeter.

Example 1:

polygon = Polygon(5, 10, 7)
Output: 50

Example 2:

polygon = Polygon(5, 10, 7)
Output: 175

Utilize the formula for the perimeter of a regular polygon (number of sides * length of one side). For the area, use the formula 1/2 * apothem * perimeter.

class Polygon:
    def __init__(self, n_sides, side_length, apothem):
        self.n_sides = n_sides
        self.side_length = side_length
        self.apothem = apothem

    def perimeter(self):
        return self.n_sides * self.side_length

    def area(self):
        return 0.5 * self.apothem * self.perimeter()

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!