find the area of a regular polygon inscribed in a circle in python

Here's a Python function to find the area of a regular polygon inscribed in a circle, given the number of sides and the radius of the circle.

main.py
import math

def area_of_inscribed_regular_polygon(n, r):
    """
    n: number of sides of the regular polygon
    r: radius of the circle
    returns: area of the regular polygon
    """
    # calculate the side length of the regular polygon
    s = 2 * r * math.sin(math.pi / n)
    
    # calculate the apothem of the regular polygon
    a = r * math.cos(math.pi / n)
    
    # calculate the area of the regular polygon
    A = 0.5 * n * s * a
    
    return A
467 chars
19 lines

To use the function, simply call it with the number of sides and the radius of the circle as arguments.

main.py
>>> area_of_inscribed_regular_polygon(6, 5)
64.9519052838329
>>> area_of_inscribed_regular_polygon(8, 10)
309.0169943749474
124 chars
5 lines

gistlibby LogSnag