unitforge/quantities/
angle.rs

1use crate::impl_macros::macros::*;
2use crate::prelude::*;
3use crate::PhysicsUnit;
4use ndarray::{Array1, Array2, ArrayView1, ArrayView2};
5use num_traits::identities::Zero;
6use num_traits::{FloatConst, FromPrimitive};
7#[cfg(feature = "pyo3")]
8use pyo3::pyclass;
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11use std::cmp::Ordering;
12use std::f64::consts::PI;
13use std::fmt;
14use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
15#[cfg(feature = "strum")]
16use strum_macros::EnumIter;
17
18#[cfg_attr(feature = "strum", derive(EnumIter))]
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20#[derive(Copy, Clone, PartialEq, Debug)]
21#[cfg_attr(feature = "pyo3", pyclass(eq, eq_int))]
22pub enum AngleUnit {
23    rad,
24    deg,
25}
26
27impl PhysicsUnit for AngleUnit {
28    fn name(&self) -> &str {
29        match &self {
30            AngleUnit::rad => "rad",
31            AngleUnit::deg => "°",
32        }
33    }
34
35    fn base_per_x(&self) -> (f64, i32) {
36        match self {
37            AngleUnit::rad => (1., 0),
38            AngleUnit::deg => (PI / 180., 0),
39        }
40    }
41}
42
43impl_const!(Angle, pi, f64::PI(), 0);
44impl_const!(Angle, perpendicular, f64::PI() / 2., 0);
45
46impl_quantity!(Angle, AngleUnit, [AngleUnit::deg]);
47impl_div_with_self_to_f64!(Angle);
48
49impl Angle {
50    pub fn sin(&self) -> f64 {
51        self.as_f64().sin()
52    }
53
54    pub fn cos(&self) -> f64 {
55        self.as_f64().cos()
56    }
57
58    pub fn tan(&self) -> f64 {
59        self.as_f64().tan()
60    }
61
62    pub fn arc_sin(value: f64) -> Self {
63        Self {
64            multiplier: value.asin(),
65            power: 0,
66        }
67    }
68
69    pub fn arc_cos(value: f64) -> Self {
70        Self {
71            multiplier: value.acos(),
72            power: 0,
73        }
74    }
75
76    pub fn arc_tan(value: f64) -> Self {
77        Self {
78            multiplier: value.atan(),
79            power: 0,
80        }
81    }
82
83    pub fn arc_tan_2(x: f64, y: f64) -> Self {
84        Self {
85            multiplier: f64::atan2(x, y),
86            power: 0,
87        }
88    }
89}