From 4c9c479ddc80d245d1ae7424cc3dd775f9ee3eef Mon Sep 17 00:00:00 2001 From: gennyble Date: Sat, 30 Nov 2024 02:24:20 -0600 Subject: Make the line solid --- src/image.rs | 36 ++++++++++++++++++++++++++++++++++-- src/main.rs | 13 ++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/image.rs b/src/image.rs index 66ca858..0ce41d9 100644 --- a/src/image.rs +++ b/src/image.rs @@ -36,11 +36,43 @@ impl Image { self.height } - pub fn rect(&mut self, x: u32, y: u32, width: u32, height: u32) { - todo!() + pub fn rect(&mut self, x: u32, y: u32, width: u32, height: u32, clr: Color) { + let x_start = x as usize; + let x_end = (x + width) as usize; + + for idx in x_start..x_end { + for idy in y as usize..y as usize + height as usize { + let data_idx = idx + idy * self.width as usize; + self.data[data_idx] = clr.into(); + } + } + } + + /// Primitive, naive line drawing funciton. lerps the points together and + /// draws a rect of the specified width at that location. + pub fn line(&mut self, x1: u32, y1: u32, x2: u32, y2: u32, width: u32, clr: Color) { + let start_x = x1.min(x2) as f32; + let start_y = y1.min(y2) as f32; + let end_x = x1.max(x2) as f32; + let end_y = y1.max(y2) as f32; + + tracing::trace!("start_x = {start_x} / end_x = {end_x}"); + tracing::trace!("start_y = {start_y} / end_y = {end_y}"); + + let dx = end_x - start_x; + let dy = end_y - start_y; + let long = dx.max(dy); + + for idx in 0..long as usize { + let x = start_x + dx * (idx as f32 / long); + let y = start_y + dy * (idx as f32 / long); + + self.rect(x as u32, y as u32, width, width, clr); + } } } +#[derive(Copy, Clone, Debug)] pub struct Color { pub r: u8, pub g: u8, diff --git a/src/main.rs b/src/main.rs index 9aed460..6d07411 100644 --- a/src/main.rs +++ b/src/main.rs @@ -169,6 +169,7 @@ impl ApplicationHandler for Etch { "ANGLE {left_angle} // {left_delta}v -=- {right_angle} // {right_delta}v" ); + let stylus_prev = self.stylus; let movement_x = left_delta / 10.0; let movement_y = right_delta / 10.0; self.stylus.x = @@ -176,9 +177,14 @@ impl ApplicationHandler for Etch { self.stylus.y = (self.stylus.y - movement_y).clamp(0.0, self.img.height() as f32); - let w = self.img.width(); - self.img.data_mut() - [w as usize * self.stylus.y as usize + self.stylus.x as usize] = LINE_COLOUR; + self.img.line( + self.stylus.x as u32, + self.stylus.y as u32, + stylus_prev.x as u32, + stylus_prev.y as u32, + 2, + LINE_COLOUR.into(), + ); tracing::info!("STYLUS: ({},{})", self.stylus.x, self.stylus.y); @@ -257,6 +263,7 @@ fn angle_delta(lhs: f32, rhs: f32) -> f32 { } } +#[derive(Copy, Clone, Debug)] pub struct Vec2 { x: f32, y: f32, -- cgit 1.4.1-3-g733a5