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

To find the surface area of a regular polygon circumscribed around a cylinder, we need to first calculate the length of each side of the polygon (also known as the apothem), as well as the height and radius of the cylinder. Once we have these values, we can use the following formula to calculate the surface area:

main.swift
SA = 2πrh + (n × s × a) / 2
28 chars
2 lines

Where:

  • SA is the surface area of the polygon-cylinder
  • π is a mathematical constant (approximately 3.14159)
  • r is the radius of the cylinder
  • h is the height of the cylinder
  • n is the number of sides of the regular polygon
  • s is the length of each side of the polygon
  • a is the apothem of the polygon

Here's how you can implement this formula in Swift:

main.swift
func surfaceArea(ofPolygonWithN sides: Int, circumscribedAroundCylinderWithRadius r: Double, andHeight h: Double) -> Double {
    let s = 2 * r * sin(Double.pi / Double(sides))
    let a = r * cos(Double.pi / Double(sides))
    let polygonArea = Double(sides) * s * a / 2
    let cylinderArea = 2 * Double.pi * r * h
    return polygonArea + cylinderArea
}
357 chars
8 lines

In this function, we first calculate the values of s and a using the formulas s = 2r*sin(π/n) and a = r*cos(π/n). We then calculate the area of the regular polygon using n, s, and a. Finally, we calculate the area of the cylinder using r and h, and add the two areas together to get the total surface area of the regular polygon circumscribed around the cylinder.

gistlibby LogSnag