Improve common types
This commit is contained in:
parent
812f246706
commit
7d412ce610
6 changed files with 170 additions and 2 deletions
42
src/common/transformation.rs
Normal file
42
src/common/transformation.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Transformation {
|
||||
rot: Direction,
|
||||
pos: Position,
|
||||
}
|
||||
|
||||
impl Transformation {
|
||||
pub fn new(rot: Direction, pos: Position) -> Self {
|
||||
Self { rot, pos }
|
||||
}
|
||||
|
||||
pub fn transform_position(&self, pos: Position) -> Position {
|
||||
(match self.rot {
|
||||
Direction::Up => pos,
|
||||
Direction::Right => Position::new(-pos.y, pos.x),
|
||||
Direction::Down => Position::new(-pos.x, -pos.y),
|
||||
Direction::Left => Position::new(pos.y, -pos.x),
|
||||
}) + self.pos
|
||||
}
|
||||
|
||||
pub fn rot(&self) -> Direction {
|
||||
self.rot
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn position() {
|
||||
let p = Position::new(3, 5);
|
||||
|
||||
let t = Transformation::new(Direction::Up, Position::new(-3, -5));
|
||||
assert_eq!(t.transform_position(p), Position::new(0, 0));
|
||||
|
||||
let t = Transformation::new(Direction::Down, Position::new(-3, -5));
|
||||
assert_eq!(t.transform_position(p), Position::new(-6, -10));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue