From 63b7836111e6e8f14126bd0151093d535bb9075b Mon Sep 17 00:00:00 2001 From: rodri Date: Thu, 9 Nov 2023 11:29:44 +0000 Subject: =?UTF-8?q?Lesson=200:=20Bresenham's=20Line=20Drawing=20Algorithm?= =?UTF-8?q?=E2=80=94Fifth=20attempt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index d6a11cd..259fa25 100644 --- a/main.c +++ b/main.c @@ -103,8 +103,7 @@ swap(int *a, int *b) void bresenham(Memimage *dst, Point p0, Point p1, Memimage *src) { - int steep = 0; - double t; + int steep = 0, Δe, e; Point p, dp; if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){ @@ -120,18 +119,19 @@ bresenham(Memimage *dst, Point p0, Point p1, Memimage *src) } dp = subpt(p1, p0); + Δe = 2*abs(dp.y); + e = 0; for(p = p0; p.x <= p1.x; p.x++){ - t = (double)(p.x-p0.x)/(p1.x-p0.x); - p.y = flerp(p0.y, p1.y, t); - - if(steep) - swap(&p.x, &p.y); - + if(steep) swap(&p.x, &p.y); pixel(dst, p, src); + if(steep) swap(&p.x, &p.y); - if(steep) - swap(&p.x, &p.y); + e += Δe; + if(e > dp.x){ + p.y += p1.y > p0.y? 1: -1; + e -= 2*dp.x; + } } } -- cgit v1.2.3