find the area of a sector of a circle in swift

To find the area of a sector of a circle in Swift, you need to know the radius of the circle and the angle of the sector in radians.

Here's a function that takes in the radius and the angle (in radians) and returns the area of the sector:

main.swift
func sectorArea(radius: Double, angle: Double) -> Double {
    let area = 0.5 * pow(radius, 2.0) * angle
    return area
}
123 chars
5 lines

Here's an example usage of the function:

main.swift
let radius = 5.0
let angle = Double.pi / 4.0 // 45 degrees in radians
let area = sectorArea(radius: radius, angle: angle)
print("The area of the sector is \(area)")
165 chars
5 lines

Output:

main.swift
The area of the sector is 3.9269908169872414
45 chars
2 lines

Note that we're using the pow function to square the radius, and Double.pi to convert the angle from degrees to radians.

gistlibby LogSnag