pub mod line_segment; pub mod polygon; pub mod rect; pub mod vec2; pub use self::line_segment::*; pub use self::polygon::*; 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. pub fn round(num: f32, step: f32) -> f32 { // Only positive steps will be accepted. assert!(step > 0.); let lower_bound = ((num / step) as i32) as f32 * step; let upper_bound = lower_bound + step; // Compare the distances and prefer the smaller. If they are the same, prefer the upper bound. if (num - lower_bound) < (upper_bound - num) { lower_bound } else { 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. fn partial_max(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. fn partial_min(a: T, b: T) -> T where T: PartialOrd, { partial_max(b, a) }