find the area of a regular polygon inscribed in a circle in swift

To find the area of a regular polygon inscribed in a circle, you can use the following formula:

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

where n is the number of sides, s is the length of each side, and pi is approximately 3.14159.

In Swift, you can implement this formula as follows:

main.swift
func areaOfRegularPolygonInscribedInCircle(numberOfSides n: Double, sideLength s: Double) -> Double {
    let pi = Double.pi
    return (n * pow(s, 2)) / (4 * tan(pi/n))
}
172 chars
5 lines

You can then call this function with the number of sides and the length of each side as arguments to calculate the area:

main.swift
let numberOfSides = 6.0
let sideLength = 5.0
let area = areaOfRegularPolygonInscribedInCircle(numberOfSides: numberOfSides, sideLength: sideLength)
print("The area of the regular polygon is \(area)")
200 chars
5 lines

This will print the area of the regular polygon inscribed in a circle with 6 sides and a side length of 5.0.

gistlibby LogSnag