find the surface area of a regular polygon circumscribed around a cylinder in python

To find the surface area of a regular polygon circumscribed around a cylinder, we need to first calculate the area of the polygon and the area of the curved surface of the cylinder. We can use the following formula:

Surface area = (Area of polygon) + (Curved surface area of cylinder)

The area of a regular polygon can be calculated using the formula:

Area = (n * s^2) / (4 * tan(pi/n))

Where n is the number of sides and s is the length of each side.

The curved surface area of a cylinder can be calculated using the formula:

Curved surface area = 2 * pi * r * h

Where r is the radius of the cylinder and h is the height of the cylinder.

Here's the Python code to implement the above formulas:

main.py
import math

def surface_area_of_polygon_circumscribing_cylinder(n, s, r, h):
    area_polygon = (n * s ** 2) / (4 * math.tan(math.pi / n))
    curved_surface_area = 2 * math.pi * r * h
    surface_area = area_polygon + curved_surface_area
    return surface_area
264 chars
8 lines

In the above code, we defined a function surface_area_of_polygon_circumscribing_cylinder that takes in the number of sides n, the length of each side s, the radius of the cylinder r, and the height of the cylinder h as input parameters. The function uses the formulas mentioned above to calculate the surface area of the regular polygon circumscribing the cylinder.

Note that the above code assumes that the polygon is circumscribing the cylinder, i.e., it is passing through the top and bottom planes of the cylinder. If the polygon is inscribed within the cylinder, then the formula to calculate the curved surface area of the cylinder would be different.

gistlibby LogSnag