Add neighbours function to position

This commit is contained in:
Joshua Coles 2023-03-04 10:32:17 +00:00
parent de88205379
commit b88b93db59
2 changed files with 15 additions and 10 deletions

View File

@ -3,6 +3,7 @@ use std::ops::Add;
pub mod walker;
pub mod storage;
pub mod model;
pub mod generic;
pub(crate) const DIM: u32 = 2;
@ -17,6 +18,15 @@ impl Position {
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 {

View File

@ -87,16 +87,11 @@ impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
}
fn check_stick(&mut self, position: &Position) -> bool {
for direction in 0..DIM {
for sign in [-1, 1] {
let neighbour = position.clone() + Position::in_direction(direction, sign);
if self.storage.at(&neighbour) && self.rng.gen_range(0.0f32..=1.0) < self.stick_probability {
return true;
}
}
}
return false;
position.neighbours()
.any(|neighbour|
self.storage.at(&neighbour)
&& self.rng.gen_range(0.0f32..=1.0) < self.stick_probability
)
}
fn spawn_particle(&mut self) {