MCPcopy Create free account
hub / github.com/endbasic/endbasic / bucket_fill

Function bucket_fill

std/src/console/drawing.rs:126–161  ·  view source on GitHub ↗

Fills the 4-connected region around `xy` via `rasops`.

(rasops: &mut R, xy: PixelsXY, fill_color: u8)

Source from the content-addressed store, hash-verified

124
125/// Fills the 4-connected region around `xy` via `rasops`.
126pub fn bucket_fill<R>(rasops: &mut R, xy: PixelsXY, fill_color: u8) -> io::Result<()>
127where
128 R: RasterOps,
129{
130 let seed_color = match rasops.peek_pixel(xy)? {
131 Some(color) => color,
132 None => return Ok(()),
133 };
134 if seed_color == fill_color {
135 return Ok(());
136 }
137
138 let mut pending = vec![xy];
139 while let Some(xy) = pending.pop() {
140 if rasops.peek_pixel(xy)? != Some(seed_color) {
141 continue;
142 }
143
144 rasops.draw_pixel(xy)?;
145
146 if xy.x > i16::MIN {
147 pending.push(PixelsXY::new(xy.x - 1, xy.y));
148 }
149 if xy.x < i16::MAX {
150 pending.push(PixelsXY::new(xy.x + 1, xy.y));
151 }
152 if xy.y > i16::MIN {
153 pending.push(PixelsXY::new(xy.x, xy.y - 1));
154 }
155 if xy.y < i16::MAX {
156 pending.push(PixelsXY::new(xy.x, xy.y + 1));
157 }
158 }
159
160 Ok(())
161}
162
163/// Draws a circle via `rasops` with `center` and `radius`.
164///

Calls 3

pushMethod · 0.80
peek_pixelMethod · 0.45
draw_pixelMethod · 0.45