Skip to main content

rscad_core/
degrees.rs

1pub trait AsDegrees: num::cast::AsPrimitive<f64> {
2    fn degrees(&self) -> Degrees {
3        Degrees::new(self.as_())
4    }
5}
6
7pub trait AsRadians: num::cast::AsPrimitive<f64> {
8    fn radians(&self) -> Radians {
9        Radians::new(self.as_())
10    }
11}
12impl<T: num::cast::AsPrimitive<f64>> AsDegrees for T {}
13impl<T: num::cast::AsPrimitive<f64>> AsRadians for T {}
14
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
17pub struct Degrees(pub f64);
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
20pub struct Radians(pub f64);
21
22pub trait Angle {
23    fn to_radians(&self) -> f64;
24    fn to_degrees(&self) -> f64;
25}
26
27impl Angle for Degrees {
28    fn to_radians(&self) -> f64 {
29        self.0.to_radians()
30    }
31
32    fn to_degrees(&self) -> f64 {
33        self.0
34    }
35}
36
37impl Angle for Radians {
38    fn to_radians(&self) -> f64 {
39        self.0
40    }
41
42    fn to_degrees(&self) -> f64 {
43        self.0.to_degrees()
44    }
45}
46
47impl Degrees {
48    pub const fn new(value: f64) -> Self {
49        Degrees(value)
50    }
51
52    pub const fn to_radians(&self) -> f64 {
53        self.0.to_radians()
54    }
55
56    pub const fn to_degrees(&self) -> f64 {
57        self.0
58    }
59}
60
61impl Radians {
62    pub const fn new(value: f64) -> Self {
63        Radians(value)
64    }
65
66    pub const fn to_radians(&self) -> f64 {
67        self.0
68    }
69
70    pub const fn to_degrees(&self) -> f64 {
71        self.0.to_degrees()
72    }
73}