Use RangeInclusive for clamp and lerp

This commit is contained in:
Emil Ernerfeldt 2020-04-25 11:14:32 +02:00
parent 8f879de9f5
commit 9ba5bea143
2 changed files with 12 additions and 12 deletions

View file

@ -450,17 +450,17 @@ impl std::fmt::Debug for Rect {
// ----------------------------------------------------------------------------
pub fn lerp<T>(min: T, max: T, t: f32) -> T
pub fn lerp<T>(range: RangeInclusive<T>, t: f32) -> T
where
f32: Mul<T, Output = T>,
T: Add<T, Output = T>,
T: Add<T, Output = T> + Copy,
{
(1.0 - t) * min + t * max
(1.0 - t) * *range.start() + t * *range.end()
}
pub fn remap(x: f32, from: RangeInclusive<f32>, to: RangeInclusive<f32>) -> f32 {
let t = (x - from.start()) / (from.end() - from.start());
lerp(*to.start(), *to.end(), t)
lerp(to, t)
}
pub fn remap_clamp(x: f32, from: RangeInclusive<f32>, to: RangeInclusive<f32>) -> f32 {
@ -471,14 +471,14 @@ pub fn remap_clamp(x: f32, from: RangeInclusive<f32>, to: RangeInclusive<f32>) -
} else {
(x - from.start()) / (from.end() - from.start())
};
lerp(*to.start(), *to.end(), t)
lerp(to, t)
}
pub fn clamp(x: f32, min: f32, max: f32) -> f32 {
if x <= min {
min
} else if x >= max {
max
pub fn clamp(x: f32, range: RangeInclusive<f32>) -> f32 {
if x <= *range.start() {
*range.start()
} else if x >= *range.end() {
*range.end()
} else {
x
}

View file

@ -133,8 +133,8 @@ impl Texture {
.round();
let texel_radius = 32.0;
let u = clamp(u, texel_radius, self.width as f32 - 1.0 - texel_radius);
let v = clamp(v, texel_radius, self.height as f32 - 1.0 - texel_radius);
let u = clamp(u, texel_radius..=self.width as f32 - 1.0 - texel_radius);
let v = clamp(v, texel_radius..=self.height as f32 - 1.0 - texel_radius);
let top_left = Vertex {
pos: zoom_rect.min(),