How to draw province borders

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 provinc…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to draw province borders." Edmond-keaton | Sciencx - Sunday October 5, 2025, https://www.scien.cx/2025/10/05/how-to-draw-province-borders/
HARVARD
Edmond-keaton | Sciencx Sunday October 5, 2025 » How to draw province borders., viewed ,<https://www.scien.cx/2025/10/05/how-to-draw-province-borders/>
VANCOUVER
Edmond-keaton | Sciencx - » How to draw province borders. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/05/how-to-draw-province-borders/
CHICAGO
" » How to draw province borders." Edmond-keaton | Sciencx - Accessed . https://www.scien.cx/2025/10/05/how-to-draw-province-borders/
IEEE
" » How to draw province borders." Edmond-keaton | Sciencx [Online]. Available: https://www.scien.cx/2025/10/05/how-to-draw-province-borders/. [Accessed: ]
rf:citation
» How to draw province borders | Edmond-keaton | Sciencx | https://www.scien.cx/2025/10/05/how-to-draw-province-borders/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.