diff options
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/mod.rs | 10 | ||||
| -rw-r--r-- | src/math/rect.rs | 42 | ||||
| -rw-r--r-- | src/math/vec2.rs | 47 |
3 files changed, 49 insertions, 50 deletions
diff --git a/src/math/mod.rs b/src/math/mod.rs index b84d270..279affc 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -11,6 +11,7 @@ pub use self::triangle::*; pub use self::vec2::*; use nalgebra::Scalar; +use num_traits::Float; use std::cmp::Ordering; /// Trait that describes an area in the vector space on the field of T @@ -31,11 +32,14 @@ pub trait Surface<T: Scalar + Copy> { /// Round a floating point number to the nearest step given by the step argument. For instance, if /// the step is 0.5, then all numbers from 0.0 to 0.24999... will be 0., while all numbers from /// 0.25 to 0.74999... will be 0.5 and so on. -pub fn round(num: f32, step: f32) -> f32 { +pub fn round<T>(num: T, step: T) -> T +where + T: Float, +{ // Only positive steps will be accepted. - assert!(step > 0.); + assert!(step > T::zero()); - let lower_bound = ((num / step) as i32) as f32 * step; + let lower_bound = (num / step).floor() * step; let upper_bound = lower_bound + step; // Compare the distances and prefer the smaller. If they are the same, prefer the upper bound. diff --git a/src/math/rect.rs b/src/math/rect.rs index 5f4e5f5..5603642 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -2,6 +2,7 @@ use super::{LineSegment, Polygon, Surface, Vec2}; //use alga::general::{Additive, Identity}; use nalgebra::{ClosedAdd, ClosedSub, RealField, Scalar}; use num_traits::identities::Zero; +use num_traits::{NumCast, ToPrimitive}; use serde::{Deserialize, Serialize}; use std::ops::{Add, AddAssign}; @@ -147,16 +148,6 @@ impl<T: Scalar + Copy + PartialOrd + ClosedAdd + ClosedSub + Zero> Surface<T> fo } // This is sad, but also sadly necessary :/ -impl<T: Into<f32> + Scalar + Copy> Into<raylib::ffi::Rectangle> for Rect<T> { - fn into(self) -> raylib::ffi::Rectangle { - raylib::ffi::Rectangle { - x: self.x.into(), - y: self.y.into(), - width: self.w.into(), - height: self.h.into(), - } - } -} impl<T: From<f32> + Scalar + Copy> From<raylib::ffi::Rectangle> for Rect<T> { fn from(r: raylib::ffi::Rectangle) -> Self { Self { @@ -167,16 +158,6 @@ impl<T: From<f32> + Scalar + Copy> From<raylib::ffi::Rectangle> for Rect<T> { } } } -impl<T: Into<f32> + Scalar + Copy> Into<raylib::math::Rectangle> for Rect<T> { - fn into(self) -> raylib::math::Rectangle { - raylib::math::Rectangle { - x: self.x.into(), - y: self.y.into(), - width: self.w.into(), - height: self.h.into(), - } - } -} impl<T: From<f32> + Scalar + Copy> From<raylib::math::Rectangle> for Rect<T> { fn from(r: raylib::math::Rectangle) -> Self { Self { @@ -188,6 +169,27 @@ impl<T: From<f32> + Scalar + Copy> From<raylib::math::Rectangle> for Rect<T> { } } +impl<T: Scalar + Copy + ToPrimitive> Into<raylib::math::Rectangle> for Rect<T> { + fn into(self) -> raylib::math::Rectangle { + raylib::math::Rectangle { + x: NumCast::from(self.x).expect("Unable to cast Rect into raylib Rect"), + y: NumCast::from(self.y).expect("Unable to cast Rect into raylib Rect"), + width: NumCast::from(self.w).expect("Unable to cast Rect into raylib Rect"), + height: NumCast::from(self.h).expect("Unable to cast Rect into raylib Rect"), + } + } +} +impl<T: Scalar + Copy + ToPrimitive> Into<raylib::ffi::Rectangle> for Rect<T> { + fn into(self) -> raylib::ffi::Rectangle { + raylib::ffi::Rectangle { + x: NumCast::from(self.x).expect("Unable to cast Rect into raylib Rect"), + y: NumCast::from(self.y).expect("Unable to cast Rect into raylib Rect"), + width: NumCast::from(self.w).expect("Unable to cast Rect into raylib Rect"), + height: NumCast::from(self.h).expect("Unable to cast Rect into raylib Rect"), + } + } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/math/vec2.rs b/src/math/vec2.rs index cd38889..d591f1d 100644 --- a/src/math/vec2.rs +++ b/src/math/vec2.rs @@ -1,7 +1,7 @@ use crate::math::Rect; use alga::general::{ClosedAdd, ClosedSub}; use nalgebra::{RealField, Scalar}; -use num_traits::One; +use num_traits::{NumCast, One, ToPrimitive}; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::convert::{From, Into}; @@ -46,18 +46,6 @@ impl<T: Scalar + Copy> Vec2<T> { } // This is sad, but also sadly necessary :/ -impl<T> Into<raylib::ffi::Vector2> for Vec2<T> -where - T: Into<f32> + Scalar + Copy, -{ - fn into(self) -> raylib::ffi::Vector2 { - raylib::ffi::Vector2 { - x: self.x.into(), - y: self.y.into(), - } - } -} - impl<T> From<raylib::ffi::Vector2> for Vec2<T> where T: From<f32> + Scalar + Copy, @@ -69,21 +57,9 @@ where } } } -impl<T: Scalar + Copy> Into<raylib::math::Vector2> for Vec2<T> -where - T: Into<f32>, -{ - fn into(self) -> raylib::math::Vector2 { - raylib::math::Vector2 { - x: self.x.into(), - y: self.y.into(), - } - } -} - -impl<T: Scalar + Copy> From<raylib::math::Vector2> for Vec2<T> +impl<T> From<raylib::math::Vector2> for Vec2<T> where - T: From<f32>, + T: From<f32> + Scalar + Copy, { fn from(v: raylib::math::Vector2) -> Self { Self { @@ -93,6 +69,23 @@ where } } +impl<T: Scalar + Copy + ToPrimitive> Into<raylib::ffi::Vector2> for Vec2<T> { + fn into(self) -> raylib::ffi::Vector2 { + raylib::ffi::Vector2 { + x: NumCast::from(self.x).expect("Unable to cast Vec2 into raylib Vector"), + y: NumCast::from(self.y).expect("Unable to cast Vec2 into raylib Vector"), + } + } +} +impl<T: Scalar + Copy + ToPrimitive> Into<raylib::math::Vector2> for Vec2<T> { + fn into(self) -> raylib::math::Vector2 { + raylib::math::Vector2 { + x: NumCast::from(self.x).expect("Unable to cast Vec2 into raylib Vector"), + y: NumCast::from(self.y).expect("Unable to cast Vec2 into raylib Vector"), + } + } +} + // Begin mathematical operators ----------------------------------------------- // Addition |
