Skip to content
Snippets Groups Projects
Commit 8e4881f0 authored by Jonathan Flueren's avatar Jonathan Flueren
Browse files

init

parent 6a0bc4dd
Branches master
No related tags found
No related merge requests found
......@@ -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"
......
......@@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
colored = "2.0.0"
rand = "0.8.5"
union-find = "0.4.1"
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())
......
......@@ -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);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment