Skip to content

Instantly share code, notes, and snippets.

@erica
Last active November 13, 2017 02:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/8cb4b21cf0c429828fad1d8ad459b71b to your computer and use it in GitHub Desktop.
Save erica/8cb4b21cf0c429828fad1d8ad459b71b to your computer and use it in GitHub Desktop.
// Angle.swift
// SwiftPlaneGeometry
// Represents the degree of turn between two straight
// lines with a common vertex
public struct Angle : CustomStringConvertible, Equatable {
/// The pi constant, the ratio of a circle's circumference to its diameter
public static let (pi, π) = (Double.pi, Double.pi)
/// The tau constant, the ratio of a circle's circumference to its radius
public static let (tau, τ) = (2 * pi, 2 * pi)
/// The default constructor creates a zero angle
public init() { _radians = 0 }
/// Initializes with radians
public init(radians: Double) { _radians = radians }
/// Initializes with user-supplied degrees
public init(degrees: Double) { _radians = degrees * Angle.pi / 180.0 }
/// Initializes with user-supplied count of π's
public init(multiplesOfPi piCount: Double) { _radians = piCount * Angle.pi }
/// Initializes with user-supplied count of τ's
public init(multiplesOfTau tauCount: Double) { _radians = tauCount * Angle.pi }
/// Expresses angle in degrees
public var degrees: Double { return _radians * 180.0 / Angle.pi }
/// Expresses angles as a count of pi
public var multiplesOfPi: Double { return _radians / Angle.pi }
/// Expresses angles as a count of tau
public var multiplesOfTau: Double { return _radians / Angle.tau }
/// Expresses angle in (native) radians
public var radians: Double { return _radians }
/// String convertible support
public var description: String {
return "\(degrees)°, \(multiplesOfPi)π, \(radians) rads"
}
/// Compares two angles, conforming type to `Equatable`
public static func ==(lhs: Angle, rhs: Angle) -> Bool {
return lhs.radians == rhs.radians
}
/// Internal radian store
private let _radians: Double
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment