From b88b93db59f3f3ec09929cc2e5521036a488c5bb Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sat, 4 Mar 2023 10:32:17 +0000 Subject: [PATCH] Add neighbours function to position --- src/system/mod.rs | 10 ++++++++++ src/system/model.rs | 15 +++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/system/mod.rs b/src/system/mod.rs index 6f7af1d..70de893 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -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 + '_ { + 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 { diff --git a/src/system/model.rs b/src/system/model.rs index 33aeacc..28b8fc6 100644 --- a/src/system/model.rs +++ b/src/system/model.rs @@ -87,16 +87,11 @@ impl> DLASystem { } 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) {