Created
July 26, 2013 05:39
-
-
Save non/6086564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package angles | |
import spire.algebra._ | |
import scala.math.Pi | |
object Angle { | |
def apply(n: Double): Angle = | |
if (n < 0.0) new Angle(n % 360.0 + 360.0) else new Angle(n % 360.0) | |
def fromRadians(n: Double): Angle = | |
Angle(n * 360.0 / (2.0 * Pi)) | |
def fromDegrees(n: Double): Angle = | |
Angle(n) | |
implicit def order: Order[Angle] = | |
new Order[Angle] { | |
override def eqv(x: Angle, y: Angle): Boolean = x.theta == y.theta | |
def compare(x: Angle, y: Angle): Int = x compare y | |
} | |
implicit def additive: AdditiveGroup[Angle] = | |
new AdditiveGroup[Angle] { | |
def zero: Angle = new Angle(0.0) | |
def plus(x: Angle, y: Angle): Angle = x + y | |
def negate(x: Angle): Angle = -x | |
override def minus(x: Angle, y: Angle): Angle = x - y | |
} | |
} | |
class Angle(val theta: Double) extends AnyVal { lhs => | |
override def toString(): String = | |
theta.toString | |
def compare(rhs: Angle): Int = | |
lhs.theta compare rhs.theta | |
def unary_-(): Angle = | |
if (theta == 0.0) this else new Angle(360.0 - theta) | |
def +(rhs: Angle): Angle = | |
Angle(lhs.theta + rhs.theta) | |
def -(rhs: Angle): Angle = | |
Angle(lhs.theta - rhs.theta) | |
def *(rhs: Double): Angle = | |
Angle(lhs.theta * rhs) | |
def /(rhs: Double): Angle = | |
Angle(lhs.theta / rhs) | |
def %(rhs: Double): Angle = | |
Angle(lhs.theta % rhs) | |
def toDegrees: Double = lhs.theta | |
def toRadians: Double = lhs.theta * 2.0 * Pi / 360.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment