Skip to content
Snippets Groups Projects
Unverified Commit 6a0bc4dd authored by Timo Kösters's avatar Timo Kösters
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
/target
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "floodfill"
version = "0.1.0"
dependencies = [
"colored",
"rand",
]
[[package]]
name = "getrandom"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "floodfill"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
colored = "2.0.0"
rand = "0.8.5"
use crate::Vec2;
pub fn generate_grid(size: Vec2) -> Vec<Vec<u8>> {
let lines = size.x * size.y / 500;
let mut grid = vec![vec![1; size.x]; size.y];
//grid.iter_mut()
//.flat_map(|r| r.iter_mut())
//.for_each(|c| *c = rand::random::<u8>().max(1));
for _ in 0..lines {
let y = rand::random::<f32>() * size.y as f32;
let x = rand::random::<f32>() * size.x as f32;
let vy = 2.0 * rand::random::<f32>() - 1.0;
let vx = 2.0 * rand::random::<f32>() - 1.0;
let rot = (rand::random::<f32>() - 0.5) / 50.0;
draw_line(x, y, vx, vy, rot, size, &mut grid);
draw_line(x, y, -vx, -vy, -rot, size, &mut grid);
}
grid
}
fn draw_line(
mut x: f32,
mut y: f32,
mut vx: f32,
mut vy: f32,
rot: f32,
size: Vec2,
grid: &mut Vec<Vec<u8>>,
) {
let steps = 1000;
for _step in 0..steps {
if x < 0.0 || x > size.x as f32 || y < 0.0 || y > size.y as f32 {
break;
}
grid[y as usize][x as usize] = 0;
y += vy;
x += vx;
vx = vx * rot.cos() + vy * rot.sin();
vy = -vx * rot.sin() + vy * rot.cos();
}
}
// 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 grid: Vec<Vec<u8>> = generator::generate_grid(Vec2 { x: 100, y: 100 });
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!(),
}
}
/// 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) {
todo!();
}
/// Gives each region in the image a different value.
/// Example:
/// 1110111111011
/// ->
/// 2220333333044
fn segment(grid: &mut Vec<Vec<u8>>) {
todo!();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment