find the volume of a regular polygon circumscribed around a cylinder in swift

To find the volume of a regular polygon circumscribed around a cylinder, we need to first find the radius and height of the cylinder.

Let's assume that the polygon has n sides, each of length s. The radius of the circumscribing cylinder is equal to half the diagonal length of one of the polygon's faces (triangles) which is s/sin(π/n). The height of the cylinder is equal to the length of one of the sides of the polygon s.

Once we have the radius and height of the cylinder, we can calculate its volume using the formula V = πr^2h.

Here's the Swift code to implement the above steps:

main.swift
func volumeOfCircumscribedPolygon(n: Int, s: Double) -> Double {
    let radius = s / sin(Double.pi/Double(n))
    let height = s
    let volume = Double.pi * radius * radius * height
    return volume
}
204 chars
7 lines

You can call this function with the number of sides n and length of each side s of the polygon, and it will return the volume of the circumscribing cylinder.

gistlibby LogSnag