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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use std::time::Duration;

use crate::tables::{approximate_via_lookup_table, COSINE_TABLE, SINE_TABLE, TANGENT_TABLE};
use crate::{Fraction, Ranged, Zero};

/// An measurement of distance between two rays sharing a common endpoint, in
/// degrees.
///
/// `Angle::degrees(1)` is equivalent to `1/360th` of a full rotation. This type
/// is commonly used to represent the amount of rotation to perform.
///
/// Internally, this type ensures that the angle represented using a
/// [`Fraction`] is always within the range of `0..=360°` (degrees). This ensures
/// that comparing two angles is efficient and deterministic.
///
/// ```rust
/// use figures::Angle;
///
/// assert_eq!(Angle::degrees(-90).into_degrees::<f32>(), 270.);
/// assert_eq!(Angle::degrees(-90), Angle::degrees(270));
/// ```
///
/// Because this type uses a [`Fraction`] internally, it will perform integer
/// math on integer types. The order of operations may impact the amount of
/// precision the final result contains.
///
/// # Working with Radians
///
/// Because π is an irrational number, this type internally uses degrees for
/// representation. Angles represented in radians can be converted using
/// [`Angle::radians`]/[`Angle::radians_f`].
#[derive(Eq, PartialEq, PartialOrd, Ord, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Angle(Fraction);

impl Angle {
    /// Returns an angle for `degrees`, where 360 degrees is equal to one full
    /// rotation.
    ///
    /// The value will be normalized to the range of `0..360`.
    #[must_use]
    pub const fn degrees(mut degrees: i16) -> Self {
        // This implementation of clamp is const, because we can reason about
        // whole numbers better than fractions.
        if degrees < 0 {
            while degrees < 0 {
                degrees += 360;
            }
        } else {
            while degrees > 360 {
                degrees -= 360;
            }
        }
        Self(Fraction::new_whole(degrees))
    }

    /// Returns an angle for `radians`, where `2π` is equal to one full
    /// rotation.
    ///
    /// The value will be normalized to the range of `0..2π`.
    #[must_use]
    pub fn degrees_fraction(degrees: Fraction) -> Self {
        Self(degrees).clamped_to_360()
    }

    /// Returns an angle for `degrees`, where 360 degrees is equal to one full
    /// rotation.
    ///
    /// The value will be normalized to the range of `0..360`.
    #[must_use]
    pub fn degrees_f(degrees: f32) -> Self {
        Self(Fraction::from(degrees)).clamped_to_360()
    }

    /// Returns an angle for `radians`, where `2π` is equal to one full
    /// rotation.
    ///
    /// The value will be normalized to the range of `0..2π`.
    #[must_use]
    pub fn radians(radians: Fraction) -> Self {
        Self(radians / Fraction::PI * 180).clamped_to_360()
    }

    /// Returns an angle for `radians`, where `2π` is equal to one full
    /// rotation.
    ///
    /// The value will be normalized to the range of `0..2π`.
    #[must_use]
    pub fn radians_f(radians: f32) -> Self {
        Self::degrees_f(radians * 180. / std::f32::consts::PI)
    }

    /// Returns this angle as represented in radians, where `2π` is equal to one
    /// full rotation.
    #[must_use]
    pub fn into_raidans<Representation>(self) -> Representation
    where
        Representation: From<Fraction>,
    {
        Representation::from(self.0 / 180 * Fraction::PI)
    }

    /// Returns this angle as represented in radians, where `2π` is equal to one
    /// full rotation.
    #[must_use]
    pub fn into_raidans_f(self) -> f32 {
        f32::from(self.0) / 180. * std::f32::consts::PI
    }

    /// Returns this angle as represented in degrees, where 360 degrees is equal
    /// to one full rotation.
    #[must_use]
    pub fn into_degrees<Representation>(self) -> Representation
    where
        Representation: From<Fraction>,
    {
        Representation::from(self.0)
    }

    fn clamped_to_360(mut self) -> Self {
        self.clamp_to_360();
        self
    }

    fn clamp_to_360(&mut self) {
        const THREESIXTY: Fraction = Fraction::new_whole(360);
        // To check if a ratio is greater than an integer, we might end up doing
        // multiplication and division. Thus, it's better to just do a single
        // division here, and check whether the ratios are still equal.
        match self.0.cmp(&Fraction::ZERO) {
            Ordering::Greater => {
                while self.0 > THREESIXTY {
                    self.0 -= THREESIXTY;
                }
            }
            Ordering::Equal => {}
            Ordering::Less => loop {
                self.0 += THREESIXTY;

                if self.0 > Fraction::ZERO {
                    break;
                }
            },
        }
    }

    /// Calculates the sine of this angle.
    #[must_use]
    pub fn sin(&self) -> Fraction {
        approximate_via_lookup_table(self.0, &SINE_TABLE)
    }

    /// Calculates the cosine of this angle.
    #[must_use]
    pub fn cos(&self) -> Fraction {
        approximate_via_lookup_table(self.0, &COSINE_TABLE)
    }

    /// Calculates the tangent of this angle.
    #[must_use]
    pub fn tan(&self) -> Fraction {
        approximate_via_lookup_table(self.0, &TANGENT_TABLE)
    }
}

impl Ranged for Angle {
    const MAX: Self = Self(Fraction::new_whole(360));
    const MIN: Self = Self::ZERO;
}

impl Zero for Angle {
    const ZERO: Self = Self(Fraction::ZERO);

    fn is_zero(&self) -> bool {
        self.0.is_zero()
    }
}

impl From<f32> for Angle {
    fn from(value: f32) -> Self {
        Self::radians_f(value)
    }
}

impl From<Fraction> for Angle {
    fn from(value: Fraction) -> Self {
        Self::radians(value)
    }
}

impl From<i16> for Angle {
    fn from(value: i16) -> Self {
        Self::degrees(value)
    }
}

impl Debug for Angle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:0.3}\u{B0}", self.0.into_f32())
    }
}

impl Display for Angle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (whole, mut fraction) = self.0.into_compound();
        let is_non_negative = !whole.is_negative();
        let whole = whole.to_string();
        f.pad_integral(is_non_negative, "", &whole)?;
        if !fraction.is_zero() {
            if let Some(precision) = f.precision() {
                f.write_char('.')?;
                for _ in 0..precision {
                    let (digit, remainder) = (fraction * Fraction::new_whole(10)).into_compound();
                    f.write_char(char::from(
                        b'0' + u8::try_from(digit).expect("fractional value"),
                    ))?;
                    fraction = remainder;
                }
            } else if fraction > Fraction::new(1, 1000) {
                f.write_char('.')?;
                loop {
                    let (digit, remainder) = (fraction * Fraction::new_whole(10)).into_compound();
                    f.write_char(char::from(
                        b'0' + u8::try_from(digit).expect("fractional value"),
                    ))?;
                    fraction = remainder;
                    if fraction < Fraction::new(1, 1000) {
                        break;
                    }
                }
            }
        }

        f.write_str("°")
    }
}

impl Add for Angle {
    type Output = Angle;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0).clamped_to_360()
    }
}

impl AddAssign for Angle {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.clamp_to_360();
    }
}

impl Sub for Angle {
    type Output = Angle;

    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0).clamped_to_360()
    }
}

impl SubAssign for Angle {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        self.clamp_to_360();
    }
}

impl Div for Angle {
    type Output = Angle;

    fn div(self, rhs: Self) -> Self::Output {
        Self(self.0 / rhs.0).clamped_to_360()
    }
}

impl DivAssign for Angle {
    fn div_assign(&mut self, rhs: Self) {
        self.0 /= rhs.0;
        self.clamp_to_360();
    }
}

impl Mul for Angle {
    type Output = Angle;

    fn mul(self, rhs: Self) -> Self::Output {
        Self(self.0 * rhs.0).clamped_to_360()
    }
}

impl MulAssign for Angle {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
        self.clamp_to_360();
    }
}

impl Mul<Duration> for Angle {
    type Output = Angle;

    fn mul(self, rhs: Duration) -> Self::Output {
        Self(self.0 * rhs.as_secs_f32()).clamped_to_360()
    }
}

macro_rules! impl_math_ops_for_std_type {
    ($type:ident) => {
        impl Add<$type> for Angle {
            type Output = Angle;

            fn add(self, rhs: $type) -> Self::Output {
                Self(self.0 + rhs).clamped_to_360()
            }
        }

        impl AddAssign<$type> for Angle {
            fn add_assign(&mut self, rhs: $type) {
                self.0 += rhs;
                self.clamp_to_360();
            }
        }

        impl Sub<$type> for Angle {
            type Output = Angle;

            fn sub(self, rhs: $type) -> Self::Output {
                Self(self.0 - rhs).clamped_to_360()
            }
        }

        impl SubAssign<$type> for Angle {
            fn sub_assign(&mut self, rhs: $type) {
                self.0 -= rhs;
                self.clamp_to_360();
            }
        }

        impl Div<$type> for Angle {
            type Output = Angle;

            fn div(self, rhs: $type) -> Self::Output {
                Self(self.0 / rhs).clamped_to_360()
            }
        }

        impl DivAssign<$type> for Angle {
            fn div_assign(&mut self, rhs: $type) {
                self.0 /= rhs;
                self.clamp_to_360();
            }
        }

        impl Mul<$type> for Angle {
            type Output = Angle;

            fn mul(self, rhs: $type) -> Self::Output {
                Self(self.0 * rhs).clamped_to_360()
            }
        }

        impl MulAssign<$type> for Angle {
            fn mul_assign(&mut self, rhs: $type) {
                self.0 *= rhs;
                self.clamp_to_360();
            }
        }
    };
}

impl_math_ops_for_std_type!(f32);
impl_math_ops_for_std_type!(i16);

impl Neg for Angle {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self(-self.0)
    }
}

#[test]
fn angle_clamp() {
    assert_eq!(Angle::degrees(361), Angle::degrees(1));
    assert_eq!(Angle::degrees(-1), Angle::degrees(359));
    assert_eq!(Angle::degrees_f(361.), Angle::degrees_f(1.));
    assert_eq!(Angle::degrees_f(-1.), Angle::degrees_f(359.));
}

#[test]
fn angle_display() {
    assert_eq!(format!("{}", Angle::degrees(10)), "10°");
    assert_eq!(format!("{}", Angle::degrees_f(10.1001)), "10.1°");
    assert_eq!(format!("{}", Angle::degrees_f(10.101)), "10.101°");
    assert_eq!(format!("{}", Angle::degrees_f(0.125)), "0.125°");
    assert_eq!(format!("{:.3}", Angle::degrees(1)), "1°");
    assert_eq!(format!("{:.3}", Angle::degrees_f(1.1)), "1.100°");
    assert_eq!(format!("{:.3}", Angle::degrees_f(0.125)), "0.125°");
}

#[test]
fn radians_to_deg() {
    assert_eq!(Angle::radians(Fraction::PI), Angle::degrees(180));
    assert_eq!(Angle::degrees(180).into_raidans::<Fraction>(), Fraction::PI);
    assert_eq!(Angle::radians_f(std::f32::consts::PI), Angle::degrees(180));
}

#[test]
fn trig_approximation() {
    use std::f32::consts::PI;

    #[track_caller]
    fn assert_close_enough(f1: Fraction, f2: f32) {
        println!("Comparing {f1} against {f2}");
        assert!(
            (f1.into_f32() - f2).abs() < 0.000_001,
            "{f1} ({}) is not close enough to {f2}",
            f1.into_f32()
        );
    }
    // We use a lookup table for the whole portion of the degrees, and then
    // approximate the remainder by using the delta between the current degree
    // and the "next".
    assert_close_enough(Angle::degrees(0).sin(), (0. / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(0.25).sin(), (0.25 / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(0.5).sin(), (0.5 / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(0.75).sin(), (0.75 / 180. * PI).sin());
    assert_close_enough(Angle::degrees(359).sin(), (359. / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(359.25).sin(), (359.25 / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(359.5).sin(), (359.5 / 180. * PI).sin());
    assert_close_enough(Angle::degrees_f(359.75).sin(), (359.75 / 180. * PI).sin());
}