working filled triangles
This commit is contained in:
parent
9047b0196b
commit
94eaa60fde
1 changed files with 16 additions and 31 deletions
47
src/main.rs
47
src/main.rs
|
@ -133,7 +133,6 @@ fn line_verts(mut a: Point2i, mut b: Point2i) -> Vec<Point2i> {
|
||||||
let mut verts: Vec<Point2i> = Vec::new();
|
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 sign = step.signum() as i32;
|
|
||||||
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 p = if is_steep {
|
||||||
|
@ -155,38 +154,24 @@ fn triangle_lines(a: Point2i, b: Point2i, c: Point2i, color: TGAColor, fb: &mut
|
||||||
}
|
}
|
||||||
|
|
||||||
fn triangle_filled(a: Point2i, b: Point2i, c: Point2i, color: TGAColor, fb: &mut TGAImage) {
|
fn triangle_filled(a: Point2i, b: Point2i, c: Point2i, color: TGAColor, fb: &mut TGAImage) {
|
||||||
let mut verts = line_verts(a, b);
|
let mut sorted = [a, b, c];
|
||||||
verts.extend(line_verts(b, c));
|
sorted.sort_unstable_by(|a, b| a.y.cmp(&b.y));
|
||||||
verts.extend(line_verts(c, a));
|
let [a, b, c] = sorted;
|
||||||
|
|
||||||
let mut lines: BTreeMap<i32, Vec<i32>> = BTreeMap::new();
|
let total_height = c.y - a.y;
|
||||||
for vert in verts.into_iter() {
|
|
||||||
lines
|
let segment_height = b.y - a.y;
|
||||||
.entry(vert.y)
|
for y in a.y..=b.y {
|
||||||
.and_modify(|e| {
|
let dy = y - a.y;
|
||||||
e.push(vert.x);
|
let x1 = a.x + ((c.x - a.x) * dy) / total_height;
|
||||||
e.sort_unstable();
|
let x2 = a.x + ((b.x - a.x) * dy) / segment_height;
|
||||||
})
|
line(Point2i::new(x1, y), Point2i::new(x2, y), color, fb);
|
||||||
.or_insert(vec![vert.x]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut prev_y: Option<i32> = None;
|
let segment_height = c.y - b.y;
|
||||||
for (y, xs) in lines.iter_mut() {
|
for y in b.y..=c.y {
|
||||||
let len = xs.len();
|
let x1 = a.x + ((c.x - a.x) * (y - a.y)) / total_height;
|
||||||
match len {
|
let x2 = b.x + ((c.x - b.x) * (y - b.y)) / segment_height;
|
||||||
0 => {}
|
line(Point2i::new(x1, y), Point2i::new(x2, y), color, fb);
|
||||||
1 => fb.set(xs[0] as u32, *y as u32, color),
|
|
||||||
_ => {
|
|
||||||
let start = xs[0];
|
|
||||||
let end = xs[len - 1];
|
|
||||||
line(Point2i::new(start, *y), Point2i::new(end, *y), color, fb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(prev) = prev_y {
|
|
||||||
if (y - prev).abs() > 1 {
|
|
||||||
dbg!(y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prev_y = Some(*y)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue