diff options
| author | Arne Dußin | 2020-12-29 11:12:11 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-12-31 14:04:15 +0100 |
| commit | b42ddaa4bf86b782bdbc619f7d66ded41c909465 (patch) | |
| tree | 709e481bb6d1e620b0e01bb02c7335ea474658ec /src/math | |
| parent | 2d2f45df9d47db25ac5a91c8f926a025c3a5dc7a (diff) | |
| download | graf_karto-b42ddaa4bf86b782bdbc619f7d66ded41c909465.tar.gz graf_karto-b42ddaa4bf86b782bdbc619f7d66ded41c909465.zip | |
Add snapping module to replace the rigid grid snapping
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/mod.rs | 14 |
1 files changed, 12 insertions, 2 deletions
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<T>(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<T>(num: T, decimal_place: u16) -> T +where + T: RealField + Pow<u16, Output = T>, +{ + round(num, nalgebra::convert::<f64, T>(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 |
