Skip to content

Commit

Permalink
Fixed the islands
Browse files Browse the repository at this point in the history
  • Loading branch information
Mads Ravn committed Aug 8, 2023
1 parent c046f2a commit 8324ef1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
Binary file added assets/small_green_islands.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/find-islands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pixtra::utility::count_colors;
use std::path::Path;

fn main() {
let canvas = Canvas::load(Path::new("assets/green_islands.png")).unwrap();
let canvas = Canvas::load(Path::new("assets/small_green_islands.png")).unwrap();
let count = count_colors(&canvas);
for (key, value) in count.iter() {
println!("{}: {}", key, value);
Expand Down
41 changes: 18 additions & 23 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,56 +711,51 @@ impl Canvas {
self
}

//TODO: Is this still needed?
pub fn fill_with_island(mut self, x: u32, y: u32, fill_color: &Pixel) -> (Canvas, Option<Island>) {
let mut points = vec![Point {x, y}];
pub fn fill_with_island_mut(&mut self, x: u32, y: u32, fill_color: &Pixel) -> Option<Island> {
let mut points = vec![];
let find_color = self.get_pixel(x, y);
if fill_color == &find_color {
return (self, None);
return None;
}

let mut to_visit = vec![(x as i64, y as i64)];
while let Some((x, y)) = to_visit.pop() {
self.set_pixel_mut(x as u32, y as u32, fill_color);
points.push(Point { x: x as u32, y: y as u32});

for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
if self.try_get_pixel(x + dx, y + dy) == Some(&find_color) {
to_visit.push((x + dx, y + dy));
let point = Point {x: x as u32, y: y as u32};
if !points.contains(&point) {
self.set_pixel_mut(x as u32, y as u32, fill_color);
points.push(Point { x: x as u32, y: y as u32});
for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
if self.try_get_pixel(x + dx, y + dy) == Some(&find_color) {
to_visit.push((x + dx, y + dy));
}
}
}
}
let island = Some(Island {points});
(self, island)
island
}

pub fn fill_with_island_mut(&mut self, x: u32, y: u32, fill_color: &Pixel) -> Option<Island> {
let mut points = vec![Point {x, y}];


// By orlp
pub fn fill(mut self, x: u32, y: u32, fill_color: &Pixel) -> Canvas {
let find_color = self.get_pixel(x, y);
if fill_color == &find_color {
return None;
return self;
}

let mut to_visit = vec![(x as i64, y as i64)];
while let Some((x, y)) = to_visit.pop() {
self.set_pixel_mut(x as u32, y as u32, fill_color);
points.push(Point { x: x as u32, y: y as u32});

for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
if self.try_get_pixel(x + dx, y + dy) == Some(&find_color) {
to_visit.push((x + dx, y + dy));
}
}
}
let island = Some(Island {points});
island
}


self

// By orlp
pub fn fill(self, x: u32, y: u32, fill_color: &Pixel) -> Canvas {
self.fill_with_island(x, y, fill_color).0
}

/// Applies filter to entire canvas. `filter` is a function that takes a reference to the
Expand Down

0 comments on commit 8324ef1

Please sign in to comment.