float-y line function

This commit is contained in:
Joe Ardent 2025-09-05 16:51:48 -07:00
parent c3f2ae9a56
commit d7e87bdb1e

View file

@ -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;
}
}