From d7e87bdb1e84398a6204ce13d6a08dfeae1b3521 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 5 Sep 2025 16:51:48 -0700 Subject: [PATCH] float-y line function --- src/main.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8382bda..d49b7cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,13 +33,36 @@ fn main() { let h = 64; let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB); - bench(&mut framebuffer); + //bench(&mut framebuffer); + baseline(&mut framebuffer); framebuffer .write_file("framebuffer.tga", true, true) .unwrap(); } +fn baseline(framebuffer: &mut TGAImage) { + let ax = 7; + let ay = 3; + let bx = 12; + let by = 37; + let cx = 62; + let cy = 53; + + framebuffer.set(ax, ay, WHITE); + framebuffer.set(bx, by, WHITE); + framebuffer.set(cx, cy, WHITE); + + let a = Point::new(ax, ay); + let b = Point::new(bx, by); + let c = Point::new(cx, cy); + + line(a, b, framebuffer, BLUE); + line(c, b, framebuffer, GREEN); + line(c, a, framebuffer, YELLOW); + line(a, c, framebuffer, RED); +} + fn bench(fb: &mut TGAImage) { let mut rng = rand::rng(); @@ -72,13 +95,14 @@ fn line(mut a: Point, mut b: Point, fb: &mut TGAImage, color: TGAColor) { std::mem::swap(&mut a.y, &mut b.y); } + let mut y = a.y as f32; + let step = (b.y - a.y) as f32 / (b.x - a.x) as f32; for x in (a.x)..b.x { - let t = (x - a.x) as f32 / (b.x - a.x) as f32; - let y = (a.y as f32 + (b.y - a.y) as f32 * t).round_ties_even() as u32; if is_steep { - fb.set(y, x as u32, color); + fb.set(y as u32, x as u32, color); } else { - fb.set(x as u32, y, color); + fb.set(x as u32, y as u32, color); } + y += step; } }