blob: 07d518ea9c8b6f8a5aa45d6e078d9452c34fa4b8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
pub mod rect;
pub use self::rect::*;
pub mod vec2;
pub use self::vec2::*;
/// 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
}
}
|