Select Git revision
tailwind.config.js
Forked from
FS Info TU Dortmund / Infoscreen / Infoscreen
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.rs 3.14 KiB
// Aufgaben:
// 1. Code in dieser Datei verstehen
// 2. Implementiere flood_fill naiv (ohne im Internet zu gucken)
// 3. Implementiere segment naiv (ohne im Internet zu gucken)
// 4. Finde einen smarteren flood_fill algorithmus und implementiere diesen, z.B. "Span filling" auf https://en.wikipedia.org/wiki/Flood_fill
// 5. Finde einen smarteren segment algorithmus und implementiere diesen, z.B. https://en.wikipedia.org/wiki/Connected-component_labeling
use colored::Color;
use colored::Colorize;
mod generator;
#[derive(Clone, Copy)]
pub struct Vec2 {
x: usize,
y: usize,
}
fn main() {
let mut grid: Vec<Vec<u8>> = generator::generate_grid(Vec2 { x: 300, y: 300 });
visualize_grid(&grid);
segment(&mut grid);
visualize_grid(&grid);
}
/// Print the grid to stdout
fn visualize_grid(grid: &Vec<Vec<u8>>) {
for row2 in grid.chunks_exact(2) {
for x in 0..row2[0].len() {
print!(
"{}",
"▄"
.on_color(to_color(row2[0][x]))
.color(to_color(row2[1][x])),
);
}
println!();
}
}
/// Convert the value in a grid cell to a color to display
fn to_color(value: u8) -> Color {
if value == 0 {
return Color::Black;
}
match value % 6 {
1 => Color::Red,
2 => Color::Green,
3 => Color::Yellow,
4 => Color::Blue,
5 => Color::Magenta,
0 => Color::Cyan,
_ => unreachable!(),
}
}
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
/// through horizontal or vertical connections to the new value.
///
/// Example setting the new_value to 2:
/// seed
/// v
/// 1110111111011
/// ->
/// 1110222222011
fn flood_fill(grid: &mut Vec<Vec<u8>>, seed_point: Vec2, new_value: u8) {
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.
/// Example:
/// 1110111111011
/// ->
/// 2220333333044
fn segment(grid: &mut Vec<Vec<u8>>) {
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);
}
}
}
}