angular.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use crate::{Float, Numeric, Primitive};
  2. pub trait Angular<T: Numeric + Float + Primitive>: From<Degree<T>> + From<Radiant<T>> {
  3. fn degree(&self) -> T;
  4. fn radiant(&self) -> T;
  5. }
  6. #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
  7. #[repr(transparent)]
  8. pub struct Degree<T: Numeric + Float + Primitive> {
  9. value: T,
  10. }
  11. impl<T: Numeric + Float + Primitive> Degree<T> {
  12. pub fn new(value: T) -> Self {
  13. Self { value }
  14. }
  15. }
  16. impl<T: Numeric + Float + Primitive> From<Radiant<T>> for Degree<T> {
  17. fn from(source: Radiant<T>) -> Self {
  18. Degree::new(source.value * T::pi() / unsafe { T::whole(180) })
  19. }
  20. }
  21. impl<T: Numeric + Float + Primitive> Angular<T> for Degree<T> {
  22. fn degree(&self) -> T {
  23. self.value
  24. }
  25. fn radiant(&self) -> T {
  26. Radiant::from(*self).value
  27. }
  28. }
  29. #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
  30. #[repr(transparent)]
  31. pub struct Radiant<T: Numeric + Float + Primitive> {
  32. value: T,
  33. }
  34. impl<T: Numeric + Float + Primitive> Radiant<T> {
  35. pub fn new(value: T) -> Self {
  36. Self { value }
  37. }
  38. }
  39. impl<T: Numeric + Float + Primitive> From<Degree<T>> for Radiant<T> {
  40. fn from(source: Degree<T>) -> Self {
  41. Radiant::new(source.value * unsafe { T::whole(180) } / T::pi())
  42. }
  43. }
  44. impl<T: Numeric + Float + Primitive> Angular<T> for Radiant<T> {
  45. fn degree(&self) -> T {
  46. Degree::from(*self).value
  47. }
  48. fn radiant(&self) -> T {
  49. self.value
  50. }
  51. }