first wrong flake draw

This commit is contained in:
nek0 2024-10-12 03:45:16 +02:00
parent daf99064d5
commit 5a759ea096
4 changed files with 88 additions and 3 deletions

7
Cargo.lock generated
View file

@ -8,6 +8,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "c_vec"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdd7a427adc0135366d99db65b36dae9237130997e560ed61118041fb72be6e8"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -40,6 +46,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380"
dependencies = [
"bitflags",
"c_vec",
"lazy_static",
"libc",
"sdl2-sys",

View file

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.37"
sdl2 = { version = "0.37", features = ["gfx"]}

View file

@ -16,6 +16,7 @@
packageBuildInputs = with pkgs; [
SDL2
SDL2_gfx
];
inherit (import "${crate2nix}/tools.nix" { inherit pkgs; })

View file

@ -3,17 +3,22 @@ extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::Canvas;
use sdl2::video::Window;
use sdl2::gfx::primitives::DrawRenderer;
use std::time::Duration;
struct Base {
lines: Vec<Line>,
}
#[derive(Clone, Debug)]
struct Position {
x: u32,
y: u32,
}
#[derive(Clone, Debug)]
struct Line {
start: Position,
end: Position,
@ -50,6 +55,10 @@ pub fn main() {
, end: Position{x: width / 4, y: 3 * height / 4}}
]};
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();
canvas.present();
'running: loop {
if i % 255 == 0 {
flip = !flip;
@ -59,14 +68,18 @@ pub fn main() {
} else {
i = i + 1;
}
canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear();
//canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
//canvas.clear();
draw_lines(&canvas, Color::RGB(255 - i, 64, i), & base.lines);
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. } |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
Event::KeyDown { keycode: Some(Keycode::Return), .. } => {
base.lines = step(base.lines);
},
_ => {}
}
}
@ -75,3 +88,67 @@ pub fn main() {
::std::thread::sleep(Duration::from_millis(1000 / 60));
}
}
fn step (lines: Vec<Line>) -> Vec<Line> {
println!("breaking {:?}", lines);
lines.iter().fold(vec![], break_line)
}
fn break_line (mut acc: Vec<Line>, line: & Line) -> Vec<Line> {
let dx = line.end.x as i32 - line.start.x as i32;
let dy = line.end.y as i32 - line.start.y as i32;
let first = Line{
start: Position {
x : line.start.x,
y : line.start.y,
},
end: Position {
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
};
let second = Line{
start: Position {
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
end: Position {
x : line.start.x,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
};
let third = Line{
start: Position {
x : line.start.x,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
end: Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
};
let fourth = Line{
start: Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
end: Position {
x : (line.start.x as i32 + dx) as u32,
y : (line.start.y as i32 + dy) as u32,
},
};
acc.append(& mut vec![first, second, third, fourth]);
acc.to_vec()
}
fn draw_lines (canvas: & Canvas<Window>, color: Color, lines: & Vec<Line>) {
for line in lines {
canvas.line(
line.start.x as i16,
line.start.y as i16,
line.end.x as i16,
line.end.y as i16,
color
).unwrap();
}
}