1use std::f32::consts::PI;
2use std::fmt::Display;
3use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Rem, Sub, SubAssign};
4
5#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
7pub struct Degrees(pub f32);
8
9impl Degrees {
10 pub const RANGE: RangeInclusive<Self> = Self(0.0)..=Self(360.0);
12}
13
14impl PartialEq<f32> for Degrees {
15 fn eq(&self, other: &f32) -> bool {
16 self.0.eq(other)
17 }
18}
19
20impl PartialOrd<f32> for Degrees {
21 fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
22 self.0.partial_cmp(other)
23 }
24}
25
26impl From<f32> for Degrees {
27 fn from(degrees: f32) -> Self {
28 Self(degrees)
29 }
30}
31
32impl From<u8> for Degrees {
33 fn from(degrees: u8) -> Self {
34 Self(f32::from(degrees))
35 }
36}
37
38impl From<Degrees> for f32 {
39 fn from(degrees: Degrees) -> Self {
40 degrees.0
41 }
42}
43
44impl From<Degrees> for f64 {
45 fn from(degrees: Degrees) -> Self {
46 Self::from(degrees.0)
47 }
48}
49
50impl Mul<f32> for Degrees {
51 type Output = Degrees;
52
53 fn mul(self, rhs: f32) -> Self::Output {
54 Self(self.0 * rhs)
55 }
56}
57
58impl num_traits::FromPrimitive for Degrees {
59 fn from_i64(n: i64) -> Option<Self> {
60 Some(Self(n as f32))
61 }
62
63 fn from_u64(n: u64) -> Option<Self> {
64 Some(Self(n as f32))
65 }
66
67 fn from_f64(n: f64) -> Option<Self> {
68 Some(Self(n as f32))
69 }
70}
71
72impl num_traits::AsPrimitive<f32> for Degrees {
73 fn as_(self) -> f32 {
74 self.0
75 }
76}
77
78impl num_traits::AsPrimitive<f64> for Degrees {
79 fn as_(self) -> f64 {
80 f64::from(self.0)
81 }
82}
83
84#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
86pub struct Radians(pub f32);
87
88impl Radians {
89 pub const RANGE: RangeInclusive<Self> = Self(0.0)..=Self(2.0 * PI);
91
92 pub const PI: Self = Self(PI);
94}
95
96impl From<Degrees> for Radians {
97 fn from(degrees: Degrees) -> Self {
98 Self(degrees.0 * PI / 180.0)
99 }
100}
101
102impl From<f32> for Radians {
103 fn from(radians: f32) -> Self {
104 Self(radians)
105 }
106}
107
108impl From<u8> for Radians {
109 fn from(radians: u8) -> Self {
110 Self(f32::from(radians))
111 }
112}
113
114impl From<Radians> for f32 {
115 fn from(radians: Radians) -> Self {
116 radians.0
117 }
118}
119
120impl From<Radians> for f64 {
121 fn from(radians: Radians) -> Self {
122 Self::from(radians.0)
123 }
124}
125
126impl num_traits::FromPrimitive for Radians {
127 fn from_i64(n: i64) -> Option<Self> {
128 Some(Self(n as f32))
129 }
130
131 fn from_u64(n: u64) -> Option<Self> {
132 Some(Self(n as f32))
133 }
134
135 fn from_f64(n: f64) -> Option<Self> {
136 Some(Self(n as f32))
137 }
138}
139
140impl num_traits::AsPrimitive<f32> for Radians {
141 fn as_(self) -> f32 {
142 self.0
143 }
144}
145
146impl num_traits::AsPrimitive<f64> for Radians {
147 fn as_(self) -> f64 {
148 f64::from(self.0)
149 }
150}
151
152impl Sub for Radians {
153 type Output = Self;
154
155 fn sub(self, rhs: Self) -> Self::Output {
156 Self(self.0 - rhs.0)
157 }
158}
159
160impl SubAssign for Radians {
161 fn sub_assign(&mut self, rhs: Self) {
162 self.0 = self.0 - rhs.0;
163 }
164}
165
166impl Add for Radians {
167 type Output = Self;
168
169 fn add(self, rhs: Self) -> Self::Output {
170 Self(self.0 + rhs.0)
171 }
172}
173
174impl Add<Degrees> for Radians {
175 type Output = Self;
176
177 fn add(self, rhs: Degrees) -> Self::Output {
178 Self(self.0 + rhs.0.to_radians())
179 }
180}
181
182impl AddAssign for Radians {
183 fn add_assign(&mut self, rhs: Radians) {
184 self.0 = self.0 + rhs.0;
185 }
186}
187
188impl Mul for Radians {
189 type Output = Self;
190
191 fn mul(self, rhs: Radians) -> Self::Output {
192 Radians(self.0 * rhs.0)
193 }
194}
195
196impl Mul<f32> for Radians {
197 type Output = Self;
198
199 fn mul(self, rhs: f32) -> Self::Output {
200 Self(self.0 * rhs)
201 }
202}
203
204impl Mul<Radians> for f32 {
205 type Output = Radians;
206
207 fn mul(self, rhs: Radians) -> Self::Output {
208 Radians(self * rhs.0)
209 }
210}
211
212impl Div<f32> for Radians {
213 type Output = Self;
214
215 fn div(self, rhs: f32) -> Self::Output {
216 Radians(self.0 / rhs)
217 }
218}
219
220impl Div for Radians {
221 type Output = Self;
222
223 fn div(self, rhs: Self) -> Self::Output {
224 Self(self.0 / rhs.0)
225 }
226}
227
228impl Rem for Radians {
229 type Output = Self;
230
231 fn rem(self, rhs: Self) -> Self::Output {
232 Self(self.0 % rhs.0)
233 }
234}
235
236impl PartialEq<f32> for Radians {
237 fn eq(&self, other: &f32) -> bool {
238 self.0.eq(other)
239 }
240}
241
242impl PartialOrd<f32> for Radians {
243 fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
244 self.0.partial_cmp(other)
245 }
246}
247
248impl Display for Radians {
249 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
250 write!(f, "{} rad", self.0)
251 }
252}