1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod vec2;
mod mat2;

pub use self::vec2::{Vec2, Cross};
pub use self::mat2::Mat2;

use std::f32;

/// The mathematical constant Pi.
pub const PI: f32 = f32::consts::PI;

/// Clamps (limits) the value of `x` in the inclusive range from `min` to `max`.
///
/// If `x < min`, `min` is returned. If `x > max`, `max` is returned.
///
/// # Examples
///
/// ```
/// # use physics2d::math::clamp;
///
/// assert_eq!(clamp(-10.0, -3.0, 5.0), -3.0);
/// assert_eq!(clamp(1.0, -3.0, 5.0), 1.0);
/// assert_eq!(clamp(999999.0, -3.0, 5.0), 5.0);
/// ```
pub fn clamp(x: f32, min: f32, max: f32) -> f32 {
    if x < min {
        min
    } else if x > max {
        max
    } else {
        x
    }
}

/// Clamps (limits) the value of `x` in the inclusive range from `0.0` to `1.0`.
///
/// If `x < 0.0`, `0.0` is returned. If `x > 1.0`, `1.0` is returned.
///
/// # Examples
///
/// ```
/// # use physics2d::math::clamp01;
///
/// assert_eq!(clamp01(-10.0), 0.0);
/// assert_eq!(clamp01(0.12345678), 0.12345678);
/// assert_eq!(clamp01(999.8989), 1.0);
/// ```
pub fn clamp01(x: f32) -> f32 {
    clamp(x, 0.0, 1.0)
}