From 6b76414e5286fa412d721dd4bbf14de01fcec16c Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Wed, 7 Sep 2022 22:53:40 +0200 Subject: [PATCH] Add unit types for degrees and radians --- narcissus-maths/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/narcissus-maths/src/lib.rs b/narcissus-maths/src/lib.rs index 4331246..23050f8 100644 --- a/narcissus-maths/src/lib.rs +++ b/narcissus-maths/src/lib.rs @@ -22,6 +22,48 @@ pub use vec2::Vec2; pub use vec3::Vec3; pub use vec4::Vec4; +/// Unit type for an angle expressed in radians. +#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)] +pub struct Rad(f32); + +impl Rad { + pub const fn new(x: f32) -> Self { + Self(x) + } + + pub const fn as_f32(self) -> f32 { + self.0 + } +} + +/// Unit type for an angle expressed in degrees. +#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)] +pub struct Deg(f32); + +impl Deg { + pub const fn new(x: f32) -> Self { + Self(x) + } + + pub const fn as_f32(self) -> f32 { + self.0 + } +} + +impl From for Deg { + #[inline(always)] + fn from(x: Rad) -> Self { + Self(x.0.to_degrees()) + } +} + +impl From for Rad { + #[inline(always)] + fn from(x: Deg) -> Self { + Self(x.0.to_radians()) + } +} + #[macro_export] macro_rules! impl_shared { ($name:ty, $t:ty, $n:expr) => { -- 2.49.0