1 | // pathfinder/geometry/src/util.rs |
2 | // |
3 | // Copyright © 2019 The Pathfinder Project Developers. |
4 | // |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
8 | // option. This file may not be copied, modified, or distributed |
9 | // except according to those terms. |
10 | |
11 | //! Various utilities. |
12 | |
13 | use std::f32; |
14 | |
15 | pub const EPSILON: f32 = 0.001; |
16 | |
17 | /// Approximate equality. |
18 | #[inline ] |
19 | pub fn approx_eq(a: f32, b: f32) -> bool { |
20 | f32::abs(self:a - b) <= EPSILON |
21 | } |
22 | |
23 | /// Linear interpolation. |
24 | #[inline ] |
25 | pub fn lerp(a: f32, b: f32, t: f32) -> f32 { |
26 | a + (b - a) * t |
27 | } |
28 | |
29 | /// Clamping. |
30 | #[inline ] |
31 | pub fn clamp(x: f32, min_val: f32, max_val: f32) -> f32 { |
32 | f32::min(self:max_val, other:f32::max(self:min_val, other:x)) |
33 | } |
34 | |
35 | /// Divides `a` by `b`, rounding up. |
36 | #[inline ] |
37 | pub fn alignup_i32(a: i32, b: i32) -> i32 { |
38 | (a + b - 1) / b |
39 | } |
40 | |