1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Unit newtypes.

use std::f64::consts::PI;
use std::ops::{Add, Mul, Sub};

/// Newtype wrapper around a radian value.
///
/// It's so easy to forget if you're using radians or degrees.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Radians<T>(pub T);

impl Radians<f64> {
    /// Create a new radian value from a degree value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::f64::consts::PI;
    /// use pos::units::Radians;
    /// let radians = Radians::from_degrees(180.0);
    /// assert_eq!(PI, radians.0);
    /// ```
    pub fn from_degrees(degrees: f64) -> Radians<f64> {
        Radians(degrees * PI / 180.0)
    }

    /// Converts this radians value to degrees.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::f64::consts::PI;
    /// use pos::units::Radians;
    /// let degrees = Radians(PI / 2.0).to_degrees();
    /// assert_eq!(90.0, degrees);
    /// ```
    pub fn to_degrees(self) -> f64 {
        self.0 * 180.0 / PI
    }
}

impl Add for Radians<f64> {
    type Output = Radians<f64>;
    fn add(self, other: Radians<f64>) -> Radians<f64> {
        Radians(self.0 + other.0)
    }
}

impl Sub for Radians<f64> {
    type Output = Radians<f64>;
    fn sub(self, other: Radians<f64>) -> Radians<f64> {
        Radians(self.0 - other.0)
    }
}

impl Mul<Radians<f64>> for f64 {
    type Output = Radians<f64>;
    fn mul(self, other: Radians<f64>) -> Radians<f64> {
        Radians(self * other.0)
    }
}