aboutsummaryrefslogtreecommitdiff
path: root/src/bitmap.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bitmap.rs')
-rw-r--r--src/bitmap.rs37
1 files changed, 29 insertions, 8 deletions
diff --git a/src/bitmap.rs b/src/bitmap.rs
index c7e8fd4..41af745 100644
--- a/src/bitmap.rs
+++ b/src/bitmap.rs
@@ -54,8 +54,8 @@ impl Add for MapPoint {
54 type Output = Self; 54 type Output = Self;
55 fn add(self, rhs: Self) -> Self::Output { 55 fn add(self, rhs: Self) -> Self::Output {
56 MapPoint { 56 MapPoint {
57 x: self.x + rhs.x, 57 x: self.x.saturating_add(rhs.x),
58 y: self.y + rhs.y, 58 y: self.y.saturating_add(rhs.y),
59 } 59 }
60 } 60 }
61} 61}
@@ -64,8 +64,8 @@ impl Sub for MapPoint {
64 type Output = Self; 64 type Output = Self;
65 fn sub(self, rhs: Self) -> Self::Output { 65 fn sub(self, rhs: Self) -> Self::Output {
66 MapPoint { 66 MapPoint {
67 x: self.x - rhs.x, 67 x: self.x.saturating_sub(rhs.x),
68 y: self.y - rhs.y, 68 y: self.y.saturating_sub(rhs.y),
69 } 69 }
70 } 70 }
71} 71}
@@ -102,7 +102,7 @@ impl MapPoint {
102 102
103impl<T> Pixmap<T> 103impl<T> Pixmap<T>
104where 104where
105 T: Copy + Clone + Default, 105 T: Copy + Clone + Default + PartialEq,
106{ 106{
107 pub fn new(width: u32, height: u32) -> Self { 107 pub fn new(width: u32, height: u32) -> Self {
108 let data = vec![Default::default(); (width * height) as usize]; 108 let data = vec![Default::default(); (width * height) as usize];
@@ -121,7 +121,7 @@ where
121 } 121 }
122 } 122 }
123 123
124 pub fn is_inside<P: Into<MapPoint>>(&self, pt: P) -> bool { 124 pub fn contains<P: Into<MapPoint>>(&self, pt: P) -> bool {
125 let MapPoint { x, y } = pt.into(); 125 let MapPoint { x, y } = pt.into();
126 x < self.width && y < self.height 126 x < self.width && y < self.height
127 } 127 }
@@ -181,7 +181,7 @@ where
181 circle 181 circle
182 .into_iter() 182 .into_iter()
183 .flat_map(|pt| MapPoint::try_from(pt)) 183 .flat_map(|pt| MapPoint::try_from(pt))
184 .filter(|&pt| self.is_inside(pt)) 184 .filter(|&pt| self.contains(pt))
185 .collect() 185 .collect()
186 } 186 }
187 187
@@ -226,9 +226,30 @@ where
226 coordinates 226 coordinates
227 .into_iter() 227 .into_iter()
228 .flat_map(|pt| MapPoint::try_from(pt)) 228 .flat_map(|pt| MapPoint::try_from(pt))
229 .filter(|&pt| self.is_inside(pt)) 229 .filter(|&pt| self.contains(pt))
230 .collect() 230 .collect()
231 } 231 }
232
233 pub fn flood_fill(
234 &mut self,
235 start: MapPoint,
236 target: T,
237 replacement: T,
238 pts: &mut Vec<MapPoint>,
239 ) {
240 if !self.contains(start) || self.get(start) != target || self.get(start) == replacement {
241 return;
242 } else {
243 pts.push(start);
244 self.set(start, replacement);
245 for (x, y) in [(1, 0), (0, 1), (-1, 0), (0, -1)].iter() {
246 let dir = MapPoint::try_from((start.x as i64 - x, start.y as i64 + y));
247 if let Ok(pt) = dir {
248 self.flood_fill(pt, target, replacement, pts);
249 }
250 }
251 }
252 }
232} 253}
233 254
234impl Pixmap<bool> { 255impl Pixmap<bool> {