From 39d6bc989feed29572a44fc3ecefcbe73481fa29 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 3 Apr 2021 14:34:18 +0530 Subject: more functional flood fill, fix panicking mirror_about --- src/bitmap.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/bitmap.rs b/src/bitmap.rs index 6aa6b12..2bf8556 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -98,8 +98,14 @@ impl MapPoint { pub fn mirror_about(self, line: u32, axis: Axis) -> MapPoint { let MapPoint { x, y } = self; match axis { - Axis::X => MapPoint { x, y: 2 * line - y }, - Axis::Y => MapPoint { x: 2 * line - x, y }, + Axis::X => MapPoint { + x, + y: (2 * line).saturating_sub(y), + }, + Axis::Y => MapPoint { + x: (2 * line).saturating_sub(x), + y, + }, } } @@ -259,18 +265,18 @@ where } else { let last = queue.pop().unwrap(); area.insert(last); - for (x, y) in [(1, 0), (0, 1), (-1, 0), (0, -1)].iter() { - let dir = MapPoint::try_from((last.x as i64 + x, last.y as i64 + y)); - if let Ok(pt) = dir { - if self.contains(pt) + [(1, 0), (0, 1), (-1, 0), (0, -1)] + .iter() + .filter_map(|(x, y)| { + MapPoint::try_from((last.x as i64 + x, last.y as i64 + y)).ok() + }) + .filter(|&pt| { + self.contains(pt) && self.get(pt) == target && self.get(pt) != replacement && !area.contains(&pt) - { - queue.push(pt); - } - } - } + }) + .for_each(|pt| queue.push(pt)); } } } -- cgit v1.2.3