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

To find the surface area of a regular polygon that is circumscribed around a triangular prism in Swift, you can follow these steps:

  1. Determine the apothem and perimeter of the polygon:
main.swift
let numSides = 6 // number of sides in the polygon
let sideLength = 5.0 // length of each side
let apothem = sideLength / (2 * tan(Double.pi / Double(numSides))) // apothem
let perimeter = Double(numSides) * sideLength // perimeter
232 chars
5 lines
  1. Find the area of each triangle that makes up the surface area:
main.swift
let base = sideLength // base of the equilateral triangle
let height = sqrt(pow(apothem, 2) + pow(base / 2, 2)) // height of each triangle
let triangleArea = (base * height) / 2 // area of each triangle
203 chars
4 lines
  1. Calculate the total surface area:
main.swift
let prismHeight = 10.0 // height of the triangular prism
let totalSurfaceArea = (2 * triangleArea) + (perimeter * prismHeight) // total surface area
149 chars
3 lines

Putting it all together:

main.swift
let numSides = 6
let sideLength = 5.0

let apothem = sideLength / (2 * tan(Double.pi / Double(numSides)))
let perimeter = Double(numSides) * sideLength

let base = sideLength
let height = sqrt(pow(apothem, 2) + pow(base / 2, 2))
let triangleArea = (base * height) / 2

let prismHeight = 10.0
let totalSurfaceArea = (2 * triangleArea) + (perimeter * prismHeight)

print("Total surface area: \(totalSurfaceArea)") // Output: Total surface area: 305.3584930769363
461 chars
15 lines

gistlibby LogSnag