aboutsummaryrefslogtreecommitdiff
path: root/src/math/mod.rs
diff options
context:
space:
mode:
authorArne Dußin2020-11-18 22:01:04 +0100
committerArne Dußin2020-11-18 22:01:04 +0100
commitf62dabcb390d4808739745c050dfba8e2826b214 (patch)
tree5cfce91911416b5e05a22cf33fd5775683b4c19d /src/math/mod.rs
parent761c8624521db5bf338c9e7e3520085820113f28 (diff)
parent8d99501918393ea0c046d721e6fa6015fe43b70f (diff)
downloadgraf_karto-f62dabcb390d4808739745c050dfba8e2826b214.tar.gz
graf_karto-f62dabcb390d4808739745c050dfba8e2826b214.zip
Merge branch 'polygon'
Diffstat (limited to 'src/math/mod.rs')
-rw-r--r--src/math/mod.rs55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/math/mod.rs b/src/math/mod.rs
index 07d518e..0b591d7 100644
--- a/src/math/mod.rs
+++ b/src/math/mod.rs
@@ -1,9 +1,17 @@
+pub mod line_segment;
+pub mod polygon;
+pub mod polygon_graph;
pub mod rect;
-pub use self::rect::*;
-
pub mod vec2;
+
+pub use self::line_segment::*;
+pub use self::polygon::*;
+pub use self::polygon_graph::*;
+pub use self::rect::*;
pub use self::vec2::*;
+use std::cmp::Ordering;
+
/// 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.
@@ -21,3 +29,46 @@ pub fn round(num: f32, step: f32) -> f32 {
upper_bound
}
}
+
+/// 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
+/// `f32::NaN` for instance were to be passed.
+pub(crate) fn partial_max<T>(a: T, b: T) -> T
+where
+ T: PartialOrd,
+{
+ match a.partial_cmp(&b) {
+ Some(Ordering::Greater) => a,
+ _ => b,
+ }
+}
+/// Like `partial_max`, but for minimum values. Comes with the same downside, too.
+pub(crate) fn partial_min<T>(a: T, b: T) -> T
+where
+ T: PartialOrd,
+{
+ match a.partial_cmp(&b) {
+ Some(Ordering::Less) => a,
+ _ => b,
+ }
+}
+
+#[cfg(test)]
+mod test {
+ #[test]
+ fn partial_max() {
+ assert_eq!(super::partial_max(0., 0.), 0.);
+ assert_eq!(super::partial_max(-1., 1.), 1.);
+ assert_eq!(super::partial_max(-2., -1.), -1.);
+ assert_eq!(super::partial_max(2., 1.), 2.);
+ }
+
+ #[test]
+ fn partial_min() {
+ assert_eq!(super::partial_min(0., 0.), 0.);
+ assert_eq!(super::partial_min(-1., 1.), -1.);
+ assert_eq!(super::partial_min(-2., -1.), -2.);
+ assert_eq!(super::partial_min(2., 1.), 1.);
+ }
+}