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

View file

@ -1,4 +1,4 @@
use crate::belt_finding::brute_force::BruteforceBuilder;
use crate::belt_finding::{brute_force::BruteforceBuilder, common::Direction};
use crate::graph::wheighted_graph::shortest_path::dijkstra;
use crate::graph::wheighted_graph::WheightedGraph;
use crate::misc::Map;
@ -6,83 +6,10 @@ use crate::priority_queue::BinaryHeap;
use colored::{Color, Colorize};
use std::fmt::Display;
use self::common::Position;
pub mod brute_force;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Direction {
Up,
Right,
Down,
Left,
}
impl Direction {
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,
_ => {
unreachable!()
}
}
}
fn vertical(&self) -> bool {
match self {
Direction::Up => true,
Direction::Right => false,
Direction::Down => true,
Direction::Left => false,
}
}
// fn horizontal(&self) -> bool {
// !self.vertical()
// }
fn reverse(&self) -> Self {
match self {
Direction::Up => Direction::Down,
Direction::Right => Direction::Left,
Direction::Down => Direction::Up,
Direction::Left => Direction::Right,
}
}
fn clockwise(&self) -> Self {
match self {
Direction::Up => Direction::Right,
Direction::Right => Direction::Down,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
}
}
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,
}
impl Position {
pub fn new(x: usize, y: usize) -> Self {
Self { x, y }
}
}
pub mod common;
#[derive(Default, Clone, Copy)]
pub struct Field {