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::from_ratio(radians, std::f32::consts::TAU)
14 }
15
16 /// Creates an angle given an amount, and what a full turn is equal to
17 /// ```
18 /// use shapemaker::geometry::Angle;
19 ///
20 /// assert_eq!(Angle::from_ratio(0.5, 1.0).degrees() as usize, 180);
21 /// assert_eq!(Angle::from_radians(std::f32::consts::TAU).degrees() as usize, 360);
22 /// ```
23 pub fn from_ratio(amount: f32, of: f32) -> Self {
24 Self(amount * Self::TURN.0 / of)
25 }
26
27 pub fn degrees(&self) -> f32 {
28 self.0
29 }
30
31 pub fn radians(&self) -> f32 {
32 // tau better than pi, haters gonna hate <3
33 self.0 * std::f32::consts::TAU / (Self::TURN.0 / 4.0)
34 }
35
36 pub fn turns(&self) -> f32 {
37 self.0 / Self::TURN.0
38 }
39
40 pub fn without_turns(&self) -> Self {
41 Self(self.0 % Self::TURN.0)
42 }
43
44 pub fn cos_sin(&self) -> (f32, f32) {
45 let rad = self.radians();
46 (rad.cos(), rad.sin())
47 }
48}
49
50impl std::ops::Sub for Angle {
51 type Output = Angle;
52
53 fn sub(self, rhs: Self) -> Self::Output {
54 Angle(self.0 - rhs.0)
55 }
56}
57
58impl std::fmt::Display for Angle {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 write!(f, "{}deg", self.degrees())
61 }
62}