Transformible coordinates are a board trait
This commit is contained in:
parent
69cf7baa45
commit
5165708a03
38
src/board.rs
38
src/board.rs
@ -131,3 +131,41 @@ pub fn decode_png(buf: &[u8]) -> DynamicImage {
|
|||||||
reader.decode().unwrap()
|
reader.decode().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lerp(v0: f32, v1: f32, t: f32) -> f32 {
|
||||||
|
v0 + t * (v1 - v0)
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
(
|
||||||
|
inv_lerp(sx.to_coordtype(), ex.to_coordtype(), x.to_coordtype()),
|
||||||
|
inv_lerp(sy.to_coordtype(), ey.to_coordtype(), y.to_coordtype()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
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)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
34
src/main.rs
34
src/main.rs
@ -13,7 +13,7 @@ use std::sync::Mutex;
|
|||||||
|
|
||||||
mod board;
|
mod board;
|
||||||
use board::Board;
|
use board::Board;
|
||||||
use board::{ encode_png, write_board_to_file, read_board_from_file };
|
use board::{ encode_png, write_board_to_file, read_board_from_file, CoordTransformer };
|
||||||
|
|
||||||
//////////////////// Global State ////////////////////
|
//////////////////// Global State ////////////////////
|
||||||
// Don't @ me...
|
// Don't @ me...
|
||||||
@ -133,35 +133,9 @@ mod dispatch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////// Utility Functions ////////////////////
|
impl CoordTransformer<i32> for frame::Frame {
|
||||||
|
fn origin(&self) -> (i32, i32) { (self.x(), self.y()) }
|
||||||
fn lerp(v0: f32, v1: f32, t: f32) -> f32 {
|
fn extremes(&self) -> (i32, i32) { (self.x()+self.w(), self.y()+self.h()) }
|
||||||
v0 + t * (v1 - v0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inv_lerp(v0: f32, v1: f32, a: f32) -> f32 {
|
|
||||||
(a - v0) / (v1 - v0)
|
|
||||||
}
|
|
||||||
|
|
||||||
trait CoordTransformer {
|
|
||||||
fn to_coords(&self, x: i32, y: i32) -> (f32, f32);
|
|
||||||
fn from_coords(&self, x: f32, y: f32) -> (i32, i32);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CoordTransformer for frame::Frame {
|
|
||||||
fn to_coords(&self, x: i32, y: i32) -> (f32, f32) {
|
|
||||||
(
|
|
||||||
inv_lerp(self.x() as f32, (self.x() + self.w()) as f32, x as f32),
|
|
||||||
inv_lerp(self.y() as f32, (self.y() + self.h()) as f32, y as f32),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_coords(&self, x: f32, y: f32) -> (i32, i32) {
|
|
||||||
(
|
|
||||||
lerp(self.x() as f32, (self.x() + self.w()) as f32, x) as i32,
|
|
||||||
lerp(self.y() as f32, (self.y() + self.h()) as f32, y) as i32,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////// App State ////////////////////
|
//////////////////// App State ////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user