find the surface area of a regular polygon inscribed in a triangular prism in python

To find the surface area of a regular polygon that is inscribed in a triangular prism, we need to break down the problem into smaller parts.

First, we need to calculate the perimeter of the regular polygon. We can use the number of sides and the length of a side to do this.

Next, we need to find the apothem of the polygon, which is the distance from the center of the polygon to the midpoint of one of its sides. We can use trigonometry to find this value.

Once we have the perimeter and apothem, we can use the formula for the area of a regular polygon:

main.py
Area = 1/2 * Perimeter * Apothem
33 chars
2 lines

Finally, we need to calculate the surface area of the triangular prism, which involves finding the area of the two triangles and the three rectangles that make up its surface.

Here's an implementation of a Python function that takes in the number of sides, the length of a side, and the height of the prism, and returns the surface area of the inscribed regular polygon.

main.py
import math

def surface_area_regular_polygon_in_prism(sides, side_length, prism_height):
    perimeter = sides * side_length
    apothem = side_length / (2 * math.tan(math.pi / sides))
    area_polygon = 0.5 * perimeter * apothem
    
    # find area of each face of the triangular prism
    area_base = 0.5 * side_length * prism_height
    area_side_rectangles = perimeter * prism_height
    area_top_triangle = area_base
    area_bottom_triangle = area_base
    
    # add up all the areas 
    total_area = 2 * area_polygon + 2 * area_base + 3 * area_side_rectangles
    
    return total_area
598 chars
18 lines

gistlibby LogSnag