diff --git a/Cargo.lock b/Cargo.lock index 7704b0a7758eaa4ac9b20e3279175f15435fc80d..8e7a25cf8ae9f713080f938d91fedb0238e61edd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,7 @@ version = "0.1.0" dependencies = [ "colored", "rand", + "union-find", ] [[package]] @@ -106,6 +107,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "union-find" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aef317275984b19dddb358f0586a4c2e3e8244cc5b48cf65d1e109f48cc96dd" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index a262f6a37d234cd80854458195a5a506a144c232..358572bc2b68ce78663ebbfe34d74b00e3a714ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] colored = "2.0.0" rand = "0.8.5" +union-find = "0.4.1" diff --git a/src/generator.rs b/src/generator.rs index 61c55b4e224da30ee76a2b119df007fc1bf660ea..15f1a04f9f2aba37faa95ed4b1fb30816c404559 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -1,7 +1,7 @@ use crate::Vec2; pub fn generate_grid(size: Vec2) -> Vec<Vec<u8>> { - let lines = size.x * size.y / 500; + let lines = size.x * size.y / 5000; let mut grid = vec![vec![1; size.x]; size.y]; //grid.iter_mut() //.flat_map(|r| r.iter_mut()) diff --git a/src/main.rs b/src/main.rs index 196ed9c18eb9f07c2c6a4e2ba884b055432678b6..cf911044adffd347c9b00c56408234b17de6f583 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,9 @@ pub struct Vec2 { } fn main() { - let grid: Vec<Vec<u8>> = generator::generate_grid(Vec2 { x: 100, y: 100 }); + let mut grid: Vec<Vec<u8>> = generator::generate_grid(Vec2 { x: 300, y: 300 }); + visualize_grid(&grid); + segment(&mut grid); visualize_grid(&grid); } @@ -53,6 +55,20 @@ fn to_color(value: u8) -> Color { } } +fn fast_flood_fill(grid: &mut Vec<Vec<u8>>, seed_point: Vec2, new_value: u8) { + let mut colorCounter: u8 = 2; + + //let mut + + // fill with colors + for y in 0..grid.len() { + for x in 0..grid[0].len() { + + } + } + +} + /// Sets all cells in the region of the seed cell to the new_value. /// /// A region is the set of cells that had the same value as the seed cell and are connected @@ -66,7 +82,25 @@ fn to_color(value: u8) -> Color { /// 1110222222011 fn flood_fill(grid: &mut Vec<Vec<u8>>, seed_point: Vec2, new_value: u8) { - todo!(); + if grid[seed_point.y][seed_point.x] == 0 { + // point on line + return; + } + + for (y, x) in [ + (seed_point.y, seed_point.x.wrapping_sub(1)), + (seed_point.y, seed_point.x + 1), + (seed_point.y.wrapping_sub(1), seed_point.x), + (seed_point.y + 1, seed_point.x), + ] { + if x >= grid[0].len() || y >= grid.len() { + continue; + } + if grid[y][x] != new_value && grid[y][x] != 0 { + grid[y][x] = new_value; + flood_fill(grid, Vec2 { y, x }, new_value); + } + } } /// Gives each region in the image a different value. @@ -75,5 +109,15 @@ fn flood_fill(grid: &mut Vec<Vec<u8>>, seed_point: Vec2, new_value: u8) { /// -> /// 2220333333044 fn segment(grid: &mut Vec<Vec<u8>>) { - todo!(); + for y in 0..grid.len() { + for x in 0..grid[0].len() { + if grid[y][x] < 2 { + let mut next_color = rand::random::<u8>(); + if next_color < 2 { + next_color += 5 + } + flood_fill(grid, Vec2 { y, x }, next_color); + } + } + } }