This repository has no description
1/// Angle, stored in degrees
2#[derive(Debug, Clone, Copy, Default)]
3pub struct Angle(pub(crate) f32);
4
5impl Angle {
6 pub const TURN: Self = Angle(360.0);
7
8 pub fn from_degrees(degrees: f32) -> Self {
9 Self(degrees)
10 }
11
12 pub fn from_radians(radians: f32) -> Self {
13 Self(radians * Self::TURN.0 / std::f32::consts::TAU)
14 }
15
16 pub fn degrees(&self) -> f32 {
17 self.0
18 }
19
20 pub fn radians(&self) -> f32 {
21 // tau better than pi, haters gonna hate <3
22 self.0 * std::f32::consts::TAU / (Self::TURN.0 / 4.0)
23 }
24
25 pub fn turns(&self) -> f32 {
26 self.0 / Self::TURN.0
27 }
28
29 pub fn without_turns(&self) -> Self {
30 Self(self.0 % Self::TURN.0)
31 }
32
33 pub fn cos_sin(&self) -> (f32, f32) {
34 let rad = self.radians();
35 (rad.cos(), rad.sin())
36 }
37}
38
39impl std::ops::Sub for Angle {
40 type Output = Angle;
41
42 fn sub(self, rhs: Self) -> Self::Output {
43 Angle(self.0 - rhs.0)
44 }
45}
46
47impl std::fmt::Display for Angle {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 write!(f, "{}deg", self.degrees())
50 }
51}