39 lines
907 B
Rust
39 lines
907 B
Rust
use std::ops::Add;
|
|
|
|
pub mod walker;
|
|
pub mod storage;
|
|
pub mod model;
|
|
pub mod generic;
|
|
|
|
pub(crate) const DIM: u32 = 2;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Position(pub i32, pub i32);
|
|
|
|
impl Position {
|
|
pub fn abs(&self) -> f32 {
|
|
((self.0.pow(2) + self.1.pow(2)) as f32).powf(0.5)
|
|
}
|
|
|
|
pub fn in_direction(direction: u32, value: i32) -> Self {
|
|
if direction == 0 { Position(value, 0) } else { Position(0, value) }
|
|
}
|
|
|
|
pub fn neighbours(&self) -> impl Iterator<Item=Self> + '_ {
|
|
let a = (0..DIM).into_iter();
|
|
|
|
return a.flat_map(|direction| [
|
|
self.clone() + Position::in_direction(direction, 1),
|
|
self.clone() + Position::in_direction(direction, -1),
|
|
]);
|
|
}
|
|
}
|
|
|
|
impl Add for Position {
|
|
type Output = Position;
|
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
Position(self.0 + rhs.0, self.1 + rhs.1)
|
|
}
|
|
}
|