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
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(clippy::std_instead_of_core)]
#![doc = include_str!("../README.md")]
#[cfg(all(feature = "std", feature = "libm"))]
compile_error!("The `std` and `libm` features may not both be enabled simultaneously.");
use core::{f64::consts::PI, fmt::Display};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
mod ops;
/// A wrapper around a `f64` angle that is guaranteed to always be normalized.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Angle {
value: f64,
}
impl Angle {
pub const ZERO: Self = Self { value: 0.0 };
pub const PI: Self = Self { value: PI };
pub const NEG_PI: Self = Self { value: -PI };
/// Create a new [`Angle`] by normalizing the given angle in radians.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::new(PI * 5.0);
/// assert_eq!(angle.radians(), PI);
/// ```
pub fn new(radians: f64) -> Self {
let two_pi = 2.0 * PI;
let mut normalized = radians % two_pi;
if normalized > PI {
normalized -= two_pi;
} else if normalized < -PI {
normalized += two_pi;
}
Angle { value: normalized }
}
/// Create a new [`Angle`] from degrees.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::from_degrees(180.0);
/// assert_eq!(angle.radians(), PI);
/// ```
pub fn from_degrees(degrees: f64) -> Angle {
Angle::new(degrees.to_radians())
}
/// Get the angle in radians.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::new(PI);
/// assert_eq!(angle.radians(), PI);
/// ```
#[inline(always)]
pub fn radians(&self) -> f64 {
self.value
}
/// Get the angle in degrees.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::new(PI);
/// assert_eq!(angle.degrees(), 180.0);
/// ```
#[inline(always)]
pub fn degrees(&self) -> f64 {
self.value.to_degrees()
}
/// Get the absolute of this angle.
///
/// ```
/// # use radian::Angle;
/// let angle = Angle::new(-1.0);
/// assert_eq!(angle.abs().radians(), 1.0);
/// ```
#[inline(always)]
pub fn abs(&self) -> Angle {
#[cfg(feature = "std")]
return Angle {
value: self.value.abs(),
};
#[cfg(feature = "libm")]
return Angle {
value: libm::fabs(self.value),
};
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
if self.value.is_sign_positive() {
*self
} else {
-*self
}
}
/// Get the difference between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(-PI / 2.0);
/// let b = Angle::new(PI / 2.0);
/// assert_eq!(a.difference(&b).radians(), PI);
/// ```
#[inline(always)]
pub fn difference(&self, other: &Self) -> Self {
Angle::new(other.value - self.value)
}
/// Get the absolute distance between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(-PI);
/// let b = Angle::new(PI / 2.0);
/// assert_eq!(a.distance(&b).radians(), PI / 2.0);
/// ```
#[inline(always)]
pub fn distance(&self, other: &Self) -> Self {
Angle::new(self.value - other.value).abs()
}
/// Check if this angle is in a clockwise driection from another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(PI);
/// let b = Angle::new(PI / 2.0);
/// assert!(a.is_clockwise_to(&b));
/// ```
pub fn is_clockwise_to(&self, other: &Self) -> bool {
self.difference(other).value < 0.0
}
/// Check if this angle is in a counterclockwise driection from another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(PI / 2.0);
/// let b = Angle::new(PI);
/// assert!(a.is_counterclockwise_to(&b));
/// ```
pub fn is_counterclockwise_to(&self, other: &Self) -> bool {
self.difference(other).value > 0.0
}
/// Clamp the angle to a given range.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::new(PI);
/// let clamped = angle.clamp(Angle::ZERO, Angle::new(PI / 2.0));
/// assert_eq!(clamped.radians(), PI / 2.0);
/// ```
pub fn clamp(&self, min: Angle, max: Angle) -> Angle {
if self.value < min.value {
min
} else if self.value > max.value {
max
} else {
*self
}
}
/// Interpolate between this angle and another by a given factor (t).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(0.0);
/// let b = Angle::new(PI);
/// let interpolated = a.lerp(&b, 0.5);
/// assert_eq!(interpolated.radians(), PI / 2.0);
/// ```
pub fn lerp(&self, other: &Self, t: f64) -> Angle {
assert!((0.0..=1.0).contains(&t), "t must be between 0.0 and 1.0");
let difference = self.difference(other);
Angle::new(self.value + difference.value * t)
}
/// Get the opposite angle (add π).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::new(-PI / 2.0);
/// assert_eq!(angle.opposite().radians(), PI / 2.0);
/// ```
pub fn opposite(&self) -> Angle {
Angle::new(self.value + PI)
}
/// Find the midpoint angle between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(0.0);
/// let b = Angle::new(PI);
/// assert_eq!(a.midpoint(&b).radians(), PI / 2.0);
/// ```
pub fn midpoint(&self, other: &Self) -> Self {
let diff = self.difference(other).value;
Angle::new(self.value + diff / 2.0)
}
/// Get the unit vector of this angle defined as (cos(θ), sin(θ)).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::{PI, SQRT_2};
/// # use approx::assert_relative_eq;
/// let angle = Angle::new(PI / 4.0);
/// let (x, y) = angle.to_unit_vector();
/// assert_relative_eq!(x, SQRT_2 / 2.0);
/// assert_relative_eq!(y, SQRT_2 / 2.0);
/// ```
#[cfg(any(feature = "std", feature = "libm"))]
pub fn to_unit_vector(&self) -> (f64, f64) {
#[cfg(feature = "std")]
return (self.value.cos(), self.value.sin());
#[cfg(feature = "libm")]
return (libm::cos(self.value), libm::sin(self.value));
}
/// Create an angle from a unit vector (x, y).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::{PI, SQRT_2};
/// # use approx::assert_relative_eq;
/// let angle = Angle::from_unit_vector(SQRT_2 / 2.0, SQRT_2 / 2.0);
/// assert_relative_eq!(angle.radians(), PI / 4.0);
/// ```
#[cfg(any(feature = "std", feature = "libm"))]
pub fn from_unit_vector(x: f64, y: f64) -> Angle {
#[cfg(feature = "std")]
return Angle { value: y.atan2(x) };
#[cfg(feature = "libm")]
return Angle {
value: libm::atan2(y, x),
};
}
}
impl Display for Angle {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} radians", self.value)
}
}
#[cfg(feature = "ufmt")]
impl ufmt::uDisplay for Angle {
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: ufmt::uWrite + ?Sized,
{
ufmt::uwrite!(f, "{} radians", ufloat::Uf64(self.value, 4))
}
}
#[cfg(test)]
mod tests {
use super::Angle;
use core::f64::consts::PI;
use proptest::prelude::*;
#[test]
fn test_in_range() {
assert_eq!(Angle::new(PI).radians(), PI);
assert_eq!(Angle::new(-PI).radians(), -PI);
assert_eq!(Angle::new(PI / 2.0).radians(), PI / 2.0);
assert_eq!(Angle::new(-PI / 2.0).radians(), -PI / 2.0);
assert_eq!(Angle::new(0.0).radians(), 0.0);
}
#[test]
fn test_out_of_range() {
assert_eq!(Angle::new(PI + 0.1).radians(), -PI + 0.1);
assert_eq!(Angle::new(-PI - 0.1).radians(), PI - 0.1);
}
#[test]
fn test_math_ops() {
assert_eq!((Angle::PI - Angle::PI / 2.0).radians(), PI / 2.0);
assert_eq!((Angle::PI * 2.0).radians(), 0.0);
}
proptest! {
#[test]
fn prop_test_in_range(angle in -PI..=PI) {
assert_eq!(Angle::new(angle).radians(), angle);
}
#[test]
fn prop_test_above_range(angle in (PI + f64::EPSILON)..) {
assert!(Angle::new(angle).radians() < angle);
}
#[test]
fn prop_test_below_range(angle in ..PI) {
assert!(Angle::new(angle).radians() > angle);
}
#[test]
#[cfg(any(feature = "std", feature = "libm"))]
fn prop_test_from_unit_vector(x in -1000.0..1000.0, y in -1000.0..1000.0) {
let angle = Angle::from_unit_vector(x, y);
assert!(-PI <= angle.radians());
assert!(angle.radians() <= PI);
}
}
}