From b42ddaa4bf86b782bdbc619f7d66ded41c909465 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 29 Dec 2020 11:12:11 +0100 Subject: Add snapping module to replace the rigid grid snapping --- src/math/mod.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/math') diff --git a/src/math/mod.rs b/src/math/mod.rs index c9c1c6e..4035de2 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -14,7 +14,8 @@ pub use self::surface::*; pub use self::triangle::*; pub use self::vec2::*; -use num_traits::Float; +use nalgebra::RealField; +use num_traits::Pow; use std::cmp::Ordering; /// Round a floating point number to the nearest step given by the step argument. For instance, if @@ -22,7 +23,7 @@ use std::cmp::Ordering; /// 0.25 to 0.74999... will be 0.5 and so on. pub fn round(num: T, step: T) -> T where - T: Float, + T: RealField, { // Only positive steps will be accepted. assert!(step > T::zero()); @@ -38,6 +39,15 @@ where } } +/// Like round, but instead of rounding to a certain fraction, rounds to the nth decimal place instead +/// of taking a granularity. +pub fn round_nth_decimal(num: T, decimal_place: u16) -> T +where + T: RealField + Pow, +{ + round(num, nalgebra::convert::(0.1).pow(decimal_place)) +} + /// Works like `std::cmp::max`, however also allows partial comparisons. It is specifically /// designed so functions that should be able to use f32 and f64 work, eventhough these do not /// implement Ord. The downside of this function however is, that its behaviour is undefined when -- cgit v1.2.3-70-g09d2