From f1ab29519b23f896b4b5ac078b8edc9c1dd7b828 Mon Sep 17 00:00:00 2001 From: rodri Date: Thu, 9 Nov 2023 11:41:26 +0000 Subject: =?UTF-8?q?Lesson=201:=20Triangle=20rasterization=20and=20back-fac?= =?UTF-8?q?e=20culling=E2=80=94Preparation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 259fa25..8b80de1 100644 --- a/main.c +++ b/main.c @@ -103,9 +103,10 @@ swap(int *a, int *b) void bresenham(Memimage *dst, Point p0, Point p1, Memimage *src) { - int steep = 0, Δe, e; + int steep = 0, Δe, e, Δy; Point p, dp; + /* transpose the points */ if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){ steep = 1; swap(&p0.x, &p0.y); @@ -121,6 +122,7 @@ bresenham(Memimage *dst, Point p0, Point p1, Memimage *src) dp = subpt(p1, p0); Δe = 2*abs(dp.y); e = 0; + Δy = p1.y > p0.y? 1: -1; for(p = p0; p.x <= p1.x; p.x++){ if(steep) swap(&p.x, &p.y); @@ -129,12 +131,20 @@ bresenham(Memimage *dst, Point p0, Point p1, Memimage *src) e += Δe; if(e > dp.x){ - p.y += p1.y > p0.y? 1: -1; + p.y += Δy; e -= 2*dp.x; } } } +void +triangle(Memimage *dst, Point p0, Point p1, Point p2, Memimage *src) +{ + bresenham(dst, p0, p1, src); + bresenham(dst, p1, p2, src); + bresenham(dst, p2, p0, src); +} + void redraw(void) { @@ -211,6 +221,7 @@ threadmain(int argc, char *argv[]) bresenham(fb, Pt(40,40), Pt(300,300), red); bresenham(fb, Pt(80,80), Pt(100,200), red); bresenham(fb, Pt(80,80), Pt(200,100), red); + triangle(fb, Pt(30,10), Pt(45, 45), Pt(5, 100), red); display->locking = 1; unlockdisplay(display); -- cgit v1.2.3