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

To find the surface area of a regular polygon inscribed in a pyramid, we first need to find the area of the base and the area of each triangular face that connects the base to the apex. Here is a Python function that calculates the surface area of a pyramid with a regular polygon inscribed in its base:

main.py
import math

def pyramid_surface_area(n, s, h):
    # n: number of sides of the regular polygon
    # s: length of each side of the regular polygon
    # h: height of the pyramid
    
    # Calculate the area of the regular polygon
    apothem = s / (2*math.tan(math.pi/n))
    base_area = 0.5 * n * s * apothem
    
    # Calculate the area of each triangular face
    face_area = 0.5 * n * s * math.sqrt(h**2 + apothem**2)
    
    # Calculate the total surface area
    surface_area = base_area + n * face_area
    
    return surface_area
543 chars
19 lines

This function takes three arguments: n, s, and h. n is the number of sides of the regular polygon inscribed in the base of the pyramid, s is the length of each side of the polygon, and h is the height of the pyramid. The function first calculates the apothem of the polygon using the formula s / (2*tan(pi/n)). It then uses the apothem to find the area of the base (0.5 * n * s * apothem). Finally, it calculates the area of each triangular face using the formula 0.5 * n * s * sqrt(h^2 + apothem^2) and multiplies by the number of faces (n) to get the total face area. The function returns the sum of the base area and the face area as the total surface area of the pyramid with a regular polygon inscribed in its base.

gistlibby LogSnag