revert line impl

This commit is contained in:
Joe Ardent 2025-09-07 12:29:06 -07:00
parent 94eaa60fde
commit 4c7337a694

View file

@ -112,39 +112,27 @@ fn _bench(fb: &mut TGAImage) {
} }
} }
fn line(a: Point2i, b: Point2i, color: TGAColor, fb: &mut TGAImage) { fn line(mut a: Point2i, mut b: Point2i, color: TGAColor, fb: &mut TGAImage) {
let verts = line_verts(a, b);
for v in verts.into_iter() {
fb.set(v.x as u32, v.y as u32, color);
}
}
fn line_verts(mut a: Point2i, mut b: Point2i) -> Vec<Point2i> {
let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs(); let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs();
if is_steep { if is_steep {
std::mem::swap(&mut a.x, &mut a.y); std::mem::swap(&mut a.x, &mut a.y);
std::mem::swap(&mut b.x, &mut b.y); std::mem::swap(&mut b.x, &mut b.y);
} }
if a.x > b.x { if a.x > b.x {
std::mem::swap(&mut a.x, &mut b.x); std::mem::swap(&mut a, &mut b);
std::mem::swap(&mut a.y, &mut b.y);
} }
let mut verts: Vec<Point2i> = Vec::new();
let step = (b.y - a.y) as f32 / (b.x - a.x) as f32; let step = (b.y - a.y) as f32 / (b.x - a.x) as f32;
let mut y = a.y as f32; let mut y = a.y as f32;
for x in (a.x)..b.x { for x in (a.x)..b.x {
let p = if is_steep { let (px, py) = if is_steep {
Point2i::new(y.round_ties_even() as i32, x) (y.round_ties_even() as i32, x)
} else { } else {
Point2i::new(x, y.round_ties_even() as i32) (x, y.round_ties_even() as i32)
}; };
verts.push(p); fb.set(px as u32, py as u32, color);
y += step; y += step;
} }
verts
} }
fn triangle_lines(a: Point2i, b: Point2i, c: Point2i, color: TGAColor, fb: &mut TGAImage) { fn triangle_lines(a: Point2i, b: Point2i, c: Point2i, color: TGAColor, fb: &mut TGAImage) {