Refactoring layout.
This commit is contained in:
parent
1c44d7aec1
commit
e9377de01f
11 changed files with 407 additions and 320 deletions
|
|
@ -1,5 +1,9 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
pub trait Transformable {
|
||||
fn transform(&self, t: Transformation) -> Self;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Transformation {
|
||||
rot: Direction,
|
||||
|
|
@ -11,9 +15,9 @@ impl Transformation {
|
|||
Self { rot, pos }
|
||||
}
|
||||
|
||||
pub fn transform_position(&self, pos: Position) -> Position {
|
||||
pub fn transform_position(&self, pos: &Position) -> Position {
|
||||
(match self.rot {
|
||||
Direction::Up => pos,
|
||||
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),
|
||||
|
|
@ -25,6 +29,37 @@ impl Transformation {
|
|||
}
|
||||
}
|
||||
|
||||
impl Transformable for Position {
|
||||
fn transform(&self, t: Transformation) -> Self {
|
||||
(match t.rot {
|
||||
Direction::Up => *self,
|
||||
Direction::Right => Position::new(-self.y, self.x),
|
||||
Direction::Down => Position::new(-self.x, -self.y),
|
||||
Direction::Left => Position::new(self.y, -self.x),
|
||||
}) + t.pos
|
||||
}
|
||||
}
|
||||
|
||||
impl Transformable for Direction {
|
||||
fn transform(&self, t: Transformation) -> Self {
|
||||
match (t.rot, self) {
|
||||
(Direction::Up, _) => Direction::Up,
|
||||
(Direction::Right, Direction::Up) => Direction::Right,
|
||||
(Direction::Right, Direction::Right) => Direction::Down,
|
||||
(Direction::Right, Direction::Down) => Direction::Left,
|
||||
(Direction::Right, Direction::Left) => Direction::Up,
|
||||
(Direction::Down, Direction::Up) => Direction::Down,
|
||||
(Direction::Down, Direction::Right) => Direction::Left,
|
||||
(Direction::Down, Direction::Down) => Direction::Up,
|
||||
(Direction::Down, Direction::Left) => Direction::Right,
|
||||
(Direction::Left, Direction::Up) => Direction::Left,
|
||||
(Direction::Left, Direction::Right) => Direction::Up,
|
||||
(Direction::Left, Direction::Down) => Direction::Right,
|
||||
(Direction::Left, Direction::Left) => Direction::Down,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::prelude::*;
|
||||
|
|
@ -34,9 +69,9 @@ mod test {
|
|||
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));
|
||||
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));
|
||||
assert_eq!(t.transform_position(&p), Position::new(-6, -10));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue