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

To find the surface area of a regular polygon inscribed in a cylinder, we need to first calculate the perimeter and apothem of the polygon. Then, we can use the following formula to find the surface area:

surface area = perimeter * height + 2 * area of polygon

where height is the height of the cylinder.

Here is the code to calculate the surface area of a regular polygon inscribed in a cylinder:

main.py
import math

def surface_area_of_polygon_in_cylinder(num_sides, side_length, height):
    # Calculate the polygon perimeter and apothem
    perimeter = num_sides * side_length
    apothem = side_length / (2 * math.tan(math.pi / num_sides))
    
    # Calculate the polygon area and the area of the cylinder top and bottom
    polygon_area = 0.5 * perimeter * apothem
    top_bottom_area = math.pi * (side_length / (2 * math.sin(math.pi / num_sides))) ** 2
    
    # Calculate the surface area of the cylinder
    surface_area = perimeter * height + 2 * polygon_area + 2 * top_bottom_area
    
    return surface_area
618 chars
16 lines

To use this function, simply call it with the number of sides, side length, and height of the cylinder:

main.py
surface_area = surface_area_of_polygon_in_cylinder(6, 5, 10)
print(surface_area) # Output: 642.2492214274281
109 chars
3 lines

gistlibby LogSnag