Another change to Coord trait stuff

This commit is contained in:
2022-05-09 15:47:00 -05:00
parent 401560d35e
commit e97598b69f
7 changed files with 1745 additions and 75 deletions

View File

@@ -139,33 +139,21 @@ fn inv_lerp(v0: f32, v1: f32, a: f32) -> f32 {
(a - v0) / (v1 - v0)
}
pub trait CoordTransformer<I: CoordtypeConvertible> {
fn origin(&self) -> (I, I);
fn extremes(&self) -> (I, I);
fn to_coords(&self, x: I, y: I) -> (f32, f32) {
let (sx, sy) = self.origin();
let (ex, ey) = self.extremes();
pub trait CoordTransformer<I: Into<(f32, f32)> + From<(f32, f32)>> {
fn origin(&self) -> I;
fn extremes(&self) -> I;
fn to_coords(&self, pos: I) -> (f32, f32) {
let (sx, sy) = self.origin().into();
let (ex, ey) = self.extremes().into();
let (x, y) = pos.into();
(
inv_lerp(sx.to_coordtype(), ex.to_coordtype(), x.to_coordtype()),
inv_lerp(sy.to_coordtype(), ey.to_coordtype(), y.to_coordtype()),
inv_lerp(sx, ex, x),
inv_lerp(sy, ey, y),
)
}
fn from_coords(&self, x: f32, y: f32) -> (I, I) {
let (sx, sy) = self.origin();
let (ex, ey) = self.extremes();
(
CoordtypeConvertible::from_coordtype(lerp(sx.to_coordtype(), ex.to_coordtype(), x)),
CoordtypeConvertible::from_coordtype(lerp(sy.to_coordtype(), ey.to_coordtype(), y)),
)
fn from_coords(&self, x: f32, y: f32) -> I {
let (sx, sy) = self.origin().into();
let (ex, ey) = self.extremes().into();
(lerp(sx, ex, x), lerp(sy, ey, y)).into()
}
}
pub trait CoordtypeConvertible {
fn to_coordtype(self) -> f32;
fn from_coordtype(coordtype: f32) -> Self;
}
impl CoordtypeConvertible for i32 {
fn to_coordtype(self) -> f32 { self as f32 }
fn from_coordtype(coordtype: f32) -> i32 { coordtype as i32 }
}