This content originally appeared on DEV Community and was authored by Edmond-keaton
I'm making an AoH2-like strategy game. I have a provinces.bmp image where each province has a unique color. The border coordinates are currently extracted by looping through pixels and checking neighbors. How can I draw the borders and fill the provinces with color? Also, is there a better way to extract border coords?
fn draw_borders(mut commands: Commands) {
let img =
image::open("assets/maps/testmap/provinces.bmp").expect("Failed to open provinces.bmp");
let (width, height) = img.dimensions();
let mut pixels: Vec<(u8, u8, u8)> = Vec::with_capacity((width * height) as usize);
for (_, _, pixel) in img.pixels() {
pixels.push((pixel[0], pixel[1], pixel[2]))
}
let mut border_coords = Vec::new();
for y in 0..height {
for x in 0..width {
let current = pixels[(y * width + y) as usize];
let neighbors = [
(x.saturating_sub(1), y),
((x + 1).min(width - 1), y),
(x, y.saturating_sub(1)),
(x, (y + 1).min(height - 1)),
];
for &(nx, ny) in neighbors.iter() {
if pixels[(ny * width + nx) as usize] != current {
border_coords.push((x, y));
break;
}
}
}
}
let border_coords: HashSet<_> = border_coords.into_iter().collect(); // remove duplicates
// render borders
}
This content originally appeared on DEV Community and was authored by Edmond-keaton

Edmond-keaton | Sciencx (2025-10-05T22:09:13+00:00) How to draw province borders. Retrieved from https://www.scien.cx/2025/10/05/how-to-draw-province-borders-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.