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
use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};

use vek::Vec2;

/// Rotation split into it's sine and cosine parts.
///
/// This allows something to rotate infinitely.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rotation {
    /// Cosine part of the rotation.
    cos: f64,
    /// Sine part of the rotation.
    sin: f64,
}

impl Rotation {
    /// With no rotation, points to the right.
    #[inline]
    pub const fn zero() -> Self {
        let (sin, cos) = (0.0, 1.0);

        Self { sin, cos }
    }

    /// Create from radians.
    #[inline]
    pub fn from_radians(rotation: f64) -> Self {
        let (sin, cos) = rotation.sin_cos();

        Self { sin, cos }
    }

    /// Create from degrees.
    #[inline]
    pub fn from_degrees(rotation: f64) -> Self {
        Self::from_radians(rotation.to_radians())
    }

    /// Create from a direction vector.
    ///
    /// Vector is assumed to be normalized.
    #[inline]
    pub fn from_direction(dir: Vec2<f64>) -> Self {
        Self::from_radians(dir.y.atan2(dir.x))
    }

    /// Convert to radians.
    #[inline]
    pub fn to_radians(self) -> f64 {
        self.sin.atan2(self.cos)
    }

    /// Convert to degrees.
    #[inline]
    pub fn to_degrees(self) -> f64 {
        self.to_radians().to_degrees()
    }

    /// Rotate a point.
    #[inline]
    pub fn rotate(&self, point: Vec2<f64>) -> Vec2<f64> {
        point.rotated_z(self.to_radians())
    }

    /// Sine.
    #[inline]
    pub fn sin(&self) -> f64 {
        self.sin
    }

    /// Cosine.
    #[inline]
    pub fn cos(&self) -> f64 {
        self.cos
    }
}

impl Default for Rotation {
    fn default() -> Self {
        Self { cos: 1.0, sin: 0.0 }
    }
}

impl From<f64> for Rotation {
    fn from(value: f64) -> Self {
        Self::from_radians(value)
    }
}

impl AddAssign<f64> for Rotation {
    fn add_assign(&mut self, rhs: f64) {
        *self = *self + rhs;
    }
}

impl AddAssign<Self> for Rotation {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl Add<f64> for Rotation {
    type Output = Self;

    fn add(self, rhs: f64) -> Self::Output {
        if rhs.is_sign_positive() {
            self + Self::from_radians(rhs)
        } else {
            self - Self::from_radians(-rhs)
        }
    }
}

impl Add<Self> for Rotation {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            cos: self.cos * rhs.cos - self.sin * rhs.sin,
            sin: self.sin * rhs.cos + self.cos * rhs.sin,
        }
    }
}

impl SubAssign<Self> for Rotation {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl SubAssign<f64> for Rotation {
    fn sub_assign(&mut self, rhs: f64) {
        *self = *self - rhs;
    }
}

impl Sub<Self> for Rotation {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        self + -rhs
    }
}

impl Sub<f64> for Rotation {
    type Output = Self;

    fn sub(self, rhs: f64) -> Self::Output {
        if rhs.is_sign_positive() {
            self - Self::from_radians(rhs)
        } else {
            self + Self::from_radians(-rhs)
        }
    }
}

impl Neg for Rotation {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self {
            cos: self.cos,
            sin: -self.sin,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Rotation;

    /// Test different operations on rotations.
    #[test]
    fn test_ops() {
        let mut a = Rotation::from_degrees(90.0);
        let b = Rotation::from_degrees(45.0);

        assert_eq!((-a).to_degrees().round() as i16, -90);
        assert_eq!((a + b).to_degrees().round() as i16, 135);
        assert_eq!((a - b).to_degrees().round() as i16, 45);

        assert_eq!((a + 45f64.to_radians()).to_degrees().round() as i16, 135);
        assert_eq!((a + 180f64.to_radians()).to_degrees().round() as i16, -90);
        assert_eq!((a - 180f64.to_radians()).to_degrees().round() as i16, -90);
        assert_eq!((a - 90f64.to_radians()).to_degrees().round() as i16, 0);
        assert_eq!(a - 1.0, a + -1.0);

        a -= 10f64.to_radians();
        assert_eq!(a.to_degrees().round() as i16, 80);
        a += 10f64.to_radians();
        assert_eq!(a.to_degrees().round() as i16, 90);
    }
}