diff options
| author | Arne Dußin | 2021-01-06 22:50:14 +0100 |
|---|---|---|
| committer | GitHub | 2021-01-06 22:50:14 +0100 |
| commit | d8123704ea4fe4f1fb677db31ecac53c9c40096e (patch) | |
| tree | e0a365444784efaaeb1eea6373b34559b6d57fbc /src/math/mod.rs | |
| parent | 30b23db9e86fdf72a4e7de72213df274ce19123e (diff) | |
| parent | fa1afb6be3ba2d521eb0791edc0bb8e631a85327 (diff) | |
| download | graf_karto-d8123704ea4fe4f1fb677db31ecac53c9c40096e.tar.gz graf_karto-d8123704ea4fe4f1fb677db31ecac53c9c40096e.zip | |
Merge pull request #27 from LordSentox/snapping
Add snapping module to replace the rigid grid snapping
Diffstat (limited to 'src/math/mod.rs')
| -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 |
