use bevy::math::Vec2;
use std::ops::{Add, Sub};
pub trait Midpoint {
type V: Add + Sub;
fn midpoint(&self, other: Self::V) -> Self::V;
}
impl Midpoint for Vec2 {
type V = Vec2;
fn midpoint(&self, other: Self::V) -> Self::V {
let vec_to_other = other - *self;
let half = vec_to_other / 2.0;
*self + half
}
}
pub trait OptionalClamp {
type N: PartialOrd;
fn clamp_optional(&self, min: Option<Self::N>, max: Option<Self::N>) -> Self::N;
}
impl OptionalClamp for f32 {
type N = f32;
fn clamp_optional(&self, min: Option<Self::N>, max: Option<Self::N>) -> Self::N {
let mut new_val = *self;
if let Some(min) = min {
new_val = f32::max(new_val, min);
}
if let Some(max) = max {
new_val = f32::min(new_val, max);
}
new_val
}
}