orbtk_utils/
angle.rs

1use derive_more::{Add, Div, From, Mul, Sub};
2use std::f64::consts::{PI, TAU};
3
4/// The OrbTk way to handle angles
5#[derive(Add, Sub, Copy, From, Clone, Debug, PartialEq, Mul, Div)]
6pub struct Angle(f64);
7
8impl Angle {
9    // TODO: Make this const fns, when the floating point operations on const fns become stable
10    pub fn from_radians(radians: f64) -> Angle {
11        Angle(radians)
12    }
13
14    pub fn from_degrees(degrees: f64) -> Angle {
15        Angle(degrees * PI / 180.0)
16    }
17
18    /// Takes a number between 0.0 and 1.0 where 0.0 represents 0 degrees and 1.0 360 degrees
19    pub fn from_turn(turn: f64) -> Angle {
20        Angle(turn * TAU)
21    }
22
23    pub fn to_radians(self) -> f64 {
24        self.0
25    }
26
27    pub fn to_degrees(self) -> f64 {
28        self.0 * 180.0 / PI
29    }
30
31    /// Gives a number between 0.0 and 1.0 where 0.0 represents 0 degrees and 1.0 360 degrees
32    pub fn to_turn(self) -> f64 {
33        self.0 / std::f64::consts::TAU
34    }
35
36    /// Creates a `Angle` with a value of 0.0
37    pub fn zero() -> Angle {
38        Angle(0.0)
39    }
40}
41
42impl Default for Angle {
43    fn default() -> Self {
44        Self::zero()
45    }
46}