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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use prelude::*;
use super::vec2::Vec2;
use core::{Uniform, AsUniform};

/// An Angle between -PI and PI.
#[derive(Copy, Clone, PartialEq, PartialOrd)]
pub struct Angle<T: Debug + Float + NumCast = f32>(pub T);

impl<T> Angle<T> where T: Debug + Float + NumCast {
    /// Returns the angle's value in radians.
    pub fn to_radians(self: &Self) -> T {
        self.0
    }
    /// Returns the angle's value in degrees.
    pub fn to_degrees(self: &Self) -> T {
        self.0.to_degrees()
    }
    /// Returns a vector pointing in the direction of the angle.
    pub fn to_vec2(self: &Self) -> Vec2<T> {
        Vec2::from_angle(*self)
    }
    /// Creates an angle from a radians value.
    pub fn from_radians(radians: T) -> Angle<T> {
        Angle::<T>(radians)
    }
    /// Creates an angle from a degrees value.
    pub fn from_degrees(degrees: T) -> Angle<T> {
        Self::from_radians(degrees.to_radians())
    }
    /// Mutates self to its normalized representation.
    #[allow(non_snake_case)]
    pub fn normalize(self: &mut Self) -> &mut Self {
        let PI = NumCast::from(f64::consts::PI).unwrap();
        let two_PI = NumCast::from(f64::consts::PI * 2.0).unwrap();
        if self.0.abs() > PI {
            self.0 = self.0 - two_PI * ((self.0 + PI) / two_PI).floor();
        }
        self
    }
    /// Mutates self so that subtracting the target will yield the smallest directional angle between self and target.
    #[allow(non_snake_case)]
    pub fn align_with(self: &mut Self, target: &Angle<T>) -> &mut Self {

        let PI = NumCast::from(f64::consts::PI).unwrap();
        let two_PI = NumCast::from(f64::consts::PI * 2.0).unwrap();

        // normalize

        if self.0.abs() > PI {
            self.0 = self.0 - two_PI * ((self.0 + PI) / two_PI).floor();
        }

        let target_radians = if target.0.abs() > PI {
            target.0 - two_PI * ((target.0 + PI) / two_PI).floor()
        } else {
            target.0
        };

        // adjust self if self-other would exceend +/-PI

        let diff = self.0 - target_radians;

        if diff.abs() > PI {
            self.0 = self.0 - diff.signum() * two_PI;
        }

        self
    }
}

impl<T> From<T> for Angle<T> where T: Debug + Float {
    fn from(other: T) -> Angle<T> {
        Angle(other)
    }
}

impl From<Angle<f64>> for f64 {
    fn from(other: Angle<f64>) -> f64 {
        other.to_radians() as f64
    }
}

impl From<Angle<f32>> for f32 {
    fn from(other: Angle<f32>) -> f32 {
        other.to_radians() as f32
    }
}

impl<'a> From<&'a Angle<f64>> for f64 {
    fn from(other: &'a Angle<f64>) -> f64 {
        other.to_radians() as f64
    }
}

impl<'a> From<&'a Angle<f32>> for f32 {
    fn from(other: &'a Angle<f32>) -> f32 {
        other.to_radians() as f32
    }
}

impl<T> ToPrimitive for Angle<T> where T: Debug + Float {
    fn to_f64(self: &Self) -> Option<f64> {
        NumCast::from(self.to_radians())
    }
    fn to_f32(self: &Self) -> Option<f32> {
        NumCast::from(self.to_radians())
    }
    fn to_i64(self: &Self) -> Option<i64> {
        NumCast::from(self.to_radians())
    }
    fn to_u64(self: &Self) -> Option<u64> {
        let value = self.to_radians();
        if value >= T::zero() { NumCast::from(self.to_radians()) } else { None }
    }
}

impl<T> FromPrimitive for Angle<T> where T: Debug + Float {
    fn from_f64(n: f64) -> Option<Angle<T>> {
        Some(Angle(NumCast::from(n).unwrap()))
    }
    fn from_f32(n: f32) -> Option<Angle<T>> {
        Some(Angle(NumCast::from(n).unwrap()))
    }
    fn from_i64(n: i64) -> Option<Angle<T>> {
        Some(Angle(NumCast::from(n).unwrap()))
    }
    fn from_u64(n: u64) -> Option<Angle<T>> {
        Some(Angle(NumCast::from(n).unwrap()))
    }
}

impl<T> Neg for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;

    fn neg(self) -> Angle<T> {
        Angle::<T>(-self.0)
    }
}

impl<T> Add for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn add(self, other: Angle<T>) -> Angle<T> {
        Angle::<T>(self.0 + other.0)
    }
}

impl<T> AddAssign for Angle<T> where T: Debug + Float {
    fn add_assign(self: &mut Self, other: Angle<T>) {
        *self = Angle::<T>(self.0 + other.0)
    }
}

impl<T> Sub for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn sub(self, other: Angle<T>) -> Angle<T> {
        Angle::<T>(self.0 - other.0)
    }
}

impl<T> SubAssign for Angle<T> where T: Debug + Float {
    fn sub_assign(self: &mut Self, other: Angle<T>) {
        *self = Angle::<T>(self.0 - other.0)
    }
}

impl<T> Mul<Angle<T>> for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn mul(self, other: Angle<T>) -> Angle<T> {
        Angle::<T>(self.0 * other.0)
    }
}

impl<T> MulAssign for Angle<T> where T: Debug + Float {
    fn mul_assign(&mut self, other: Angle<T>) {
        *self = Angle::<T>(self.0 * other.0)
    }
}

impl<T> Mul<T> for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn mul(self, other: T) -> Angle<T> {
        Angle::<T>(self.0 * other)
    }
}

impl<T> Div<Angle<T>> for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn div(self, other: Angle<T>) -> Angle<T> {
        Angle::<T>(self.0 / other.0)
    }
}

impl<T> DivAssign for Angle<T> where T: Debug + Float {
    fn div_assign(&mut self, other: Angle<T>) {
        *self = Angle::<T>(self.0 / other.0)
    }
}

impl<T> Div<T> for Angle<T> where T: Debug + Float {
    type Output = Angle<T>;
    fn div(self, other: T) -> Angle<T> {
        Angle::<T>(self.0 / other)
    }
}

impl AsUniform for Angle<f32> {
    fn as_uniform(&self) -> Uniform {
        Uniform::Float(self.0)
    }
}

impl AsUniform for Angle<f64> {
    fn as_uniform(&self) -> Uniform {
        Uniform::Double(self.0)
    }
}

impl<T> Debug for Angle<T> where T: Debug + Float {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Angle({:?})", self.0)
    }
}