Skip to content

Instantly share code, notes, and snippets.

@mitchellporter
Last active August 29, 2015 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchellporter/644df8bbc9fb57775cfa to your computer and use it in GitHub Desktop.
Save mitchellporter/644df8bbc9fb57775cfa to your computer and use it in GitHub Desktop.
Angle.swift
// Playground - noun: a place where people can play
import Foundation
func DegreesToRadians (value:Double) -> Double {
return value * M_PI / 180.0
}
func RadiansToDegrees (value:Double) -> Double {
return value * 180.0 / M_PI
}
enum Angle {
case Radians(Double)
case Degrees(Double)
var scalar: Double {
switch(self) {
case .Radians(let v):
return v
case .Degrees(let v):
return v
}
}
var radians: Angle {
switch(self) {
case .Radians(_):
return self
case .Degrees(let value):
return .Radians(DegreesToRadians(value))
}
}
var degrees: Angle {
switch(self) {
case .Degrees(let value):
return .Degrees(RadiansToDegrees(value))
case .Radians(_):
return self
}
}
}
extension Double {
init(_ angle:Angle) {
self.init(angle.scalar)
}
}
sizeof(Double)
sizeof(Angle)
let a = Angle.Degrees(90)
a.scalar
Double(a.radians)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment