diff options
author | Akshay <[email protected]> | 2021-04-03 10:04:18 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-04-03 10:04:18 +0100 |
commit | 39d6bc989feed29572a44fc3ecefcbe73481fa29 (patch) | |
tree | 47548110be4bb83cf868e5d27cafde539fde3246 /src | |
parent | 2b89b786a97b788ef8063b2777c6bc65a207033f (diff) |
more functional flood fill, fix panicking mirror_about
Diffstat (limited to 'src')
-rw-r--r-- | src/bitmap.rs | 28 |
1 files 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 { | |||
98 | pub fn mirror_about(self, line: u32, axis: Axis) -> MapPoint { | 98 | pub fn mirror_about(self, line: u32, axis: Axis) -> MapPoint { |
99 | let MapPoint { x, y } = self; | 99 | let MapPoint { x, y } = self; |
100 | match axis { | 100 | match axis { |
101 | Axis::X => MapPoint { x, y: 2 * line - y }, | 101 | Axis::X => MapPoint { |
102 | Axis::Y => MapPoint { x: 2 * line - x, y }, | 102 | x, |
103 | y: (2 * line).saturating_sub(y), | ||
104 | }, | ||
105 | Axis::Y => MapPoint { | ||
106 | x: (2 * line).saturating_sub(x), | ||
107 | y, | ||
108 | }, | ||
103 | } | 109 | } |
104 | } | 110 | } |
105 | 111 | ||
@@ -259,18 +265,18 @@ where | |||
259 | } else { | 265 | } else { |
260 | let last = queue.pop().unwrap(); | 266 | let last = queue.pop().unwrap(); |
261 | area.insert(last); | 267 | area.insert(last); |
262 | for (x, y) in [(1, 0), (0, 1), (-1, 0), (0, -1)].iter() { | 268 | [(1, 0), (0, 1), (-1, 0), (0, -1)] |
263 | let dir = MapPoint::try_from((last.x as i64 + x, last.y as i64 + y)); | 269 | .iter() |
264 | if let Ok(pt) = dir { | 270 | .filter_map(|(x, y)| { |
265 | if self.contains(pt) | 271 | MapPoint::try_from((last.x as i64 + x, last.y as i64 + y)).ok() |
272 | }) | ||
273 | .filter(|&pt| { | ||
274 | self.contains(pt) | ||
266 | && self.get(pt) == target | 275 | && self.get(pt) == target |
267 | && self.get(pt) != replacement | 276 | && self.get(pt) != replacement |
268 | && !area.contains(&pt) | 277 | && !area.contains(&pt) |
269 | { | 278 | }) |
270 | queue.push(pt); | 279 | .for_each(|pt| queue.push(pt)); |
271 | } | ||
272 | } | ||
273 | } | ||
274 | } | 280 | } |
275 | } | 281 | } |
276 | } | 282 | } |