find the surface area of a regular polygon inscribed in a sphere in python

To find the surface area of a regular polygon inscribed in a sphere, we need to use the formula:

main.py
area = (number of sides * side length^2) / (4 * tan(pi/number of sides))
73 chars
2 lines

Where number of sides is the number of sides of the polygon, side length is the length of each side, and pi is the mathematical constant pi.

To calculate the side length of the regular polygon, we can use the formula:

main.py
side length = 2 * radius * sin(pi/number of sides)
51 chars
2 lines

Where radius is the radius of the sphere.

Putting these two formulas together, we can write a Python function to calculate the surface area of a regular polygon inscribed in a sphere:

main.py
import math

def surface_area_of_polygon_in_sphere(radius, num_sides):
    side_length = 2 * radius * math.sin(math.pi/num_sides)
    area = (num_sides * side_length ** 2) / (4 * math.tan(math.pi/num_sides))
    surface_area = area * num_sides
    return surface_area
268 chars
8 lines

Here, we use the math module to access the pi, sin, tan, and sqrt functions. We take in the radius and num_sides as input parameters and use the formulas described above to calculate the surface area of the polygon. Finally, we return surface_area as the output of the function.

gistlibby LogSnag