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
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[derive(Copy, Clone, PartialEq, Debug)]
18#[cfg_attr(feature = "pyo3", pyclass(eq, eq_int))]
19pub enum AngleUnit {
20    rad,
21    deg,
22}
23
24impl PhysicsUnit for AngleUnit {
25    fn name(&self) -> &str {
26        match &self {
27            AngleUnit::rad => "rad",
28            AngleUnit::deg => "°",
29        }
30    }
31
32    fn base_per_x(&self) -> (f64, i32) {
33        match self {
34            AngleUnit::rad => (1., 0),
35            AngleUnit::deg => (PI / 180., 0),
36        }
37    }
38}
39
40impl_const!(Angle, pi, f64::PI(), 0);
41impl_const!(Angle, perpendicular, f64::PI() / 2., 0);
42
43impl_quantity!(Angle, AngleUnit, AngleUnit::deg);
44impl_div_with_self_to_f64!(Angle);
45
46impl Angle {
47    pub fn sin(&self) -> f64 {
48        self.as_f64().sin()
49    }
50
51    pub fn cos(&self) -> f64 {
52        self.as_f64().cos()
53    }
54
55    pub fn tan(&self) -> f64 {
56        self.as_f64().tan()
57    }
58
59    pub fn arc_sin(value: f64) -> Self {
60        Self {
61            multiplier: value.asin(),
62            power: 0,
63        }
64    }
65
66    pub fn arc_cos(value: f64) -> Self {
67        Self {
68            multiplier: value.acos(),
69            power: 0,
70        }
71    }
72
73    pub fn arc_tan(value: f64) -> Self {
74        Self {
75            multiplier: value.atan(),
76            power: 0,
77        }
78    }
79
80    pub fn arc_tan_2(x: f64, y: f64) -> Self {
81        Self {
82            multiplier: f64::atan2(x, y),
83            power: 0,
84        }
85    }
86}