Refactor Direction and Position.

This commit is contained in:
hal8174 2024-01-14 14:28:45 +01:00
parent 2bdfd8cc6c
commit 9751764611
6 changed files with 206 additions and 220 deletions

146
src/belt_finding/common.rs Normal file
View file

@ -0,0 +1,146 @@
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Direction {
Up,
Right,
Down,
Left,
}
impl Direction {
pub fn from_neghbors(pos: &Position, neighbor: &Position) -> Self {
let x_diff = pos.x as i64 - neighbor.x as i64;
let y_diff = pos.y as i64 - neighbor.y as i64;
match (x_diff, y_diff) {
(1, 0) => Direction::Left,
(0, 1) => Direction::Up,
(-1, 0) => Direction::Right,
(0, -1) => Direction::Down,
_ => {
panic!("Positions are not neighbors.")
}
}
}
pub fn vertical(&self) -> bool {
match self {
Direction::Up => true,
Direction::Right => false,
Direction::Down => true,
Direction::Left => false,
}
}
pub fn horizontal(&self) -> bool {
!self.vertical()
}
pub fn reverse(&self) -> Self {
match self {
Direction::Up => Direction::Down,
Direction::Right => Direction::Left,
Direction::Down => Direction::Up,
Direction::Left => Direction::Right,
}
}
pub fn clockwise(&self) -> Self {
match self {
Direction::Up => Direction::Right,
Direction::Right => Direction::Down,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
}
}
pub fn counter_clockwise(&self) -> Self {
match self {
Direction::Up => Direction::Left,
Direction::Right => Direction::Up,
Direction::Down => Direction::Right,
Direction::Left => Direction::Down,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Position {
pub x: usize,
pub y: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Dimension {
pub width: usize,
pub height: usize,
}
impl Position {
pub fn new(x: usize, y: usize) -> Self {
Self { x, y }
}
pub fn in_direction(
&self,
dir: &Direction,
len: usize,
dimension: &Dimension,
) -> Option<Position> {
match dir {
Direction::Up => self.y.checked_sub(len).map(|y| Position::new(self.x, y)),
Direction::Right => Some(self.x + len)
.filter(|&x| x < dimension.width)
.map(|x| Position::new(x, self.y)),
Direction::Down => Some(self.y + len)
.filter(|&y| y < dimension.height)
.map(|y| Position::new(self.x, y)),
Direction::Left => self.x.checked_sub(len).map(|x| Position::new(x, self.y)),
}
}
}
#[derive(Clone, Debug, Copy)]
pub enum PathField {
Belt {
pos: Position,
dir: Direction,
},
Underground {
pos: Position,
dir: Direction,
len: u8,
},
}
impl PathField {
pub fn pos(&self) -> &Position {
match self {
PathField::Belt { pos, dir: _ } => pos,
PathField::Underground {
pos,
dir: _,
len: _,
} => pos,
}
}
pub fn dir(&self) -> &Direction {
match self {
PathField::Belt { pos: _, dir } => dir,
PathField::Underground {
pos: _,
dir,
len: _,
} => dir,
}
}
pub fn end_pos(&self, dimension: &Dimension) -> Option<(Position, Direction)> {
match self {
PathField::Belt { pos, dir } => Some((*pos, *dir)),
PathField::Underground { pos, dir, len } => pos
.in_direction(dir, *len as usize, dimension)
.map(|p| (p, *dir)),
}
}
}