ref: 63b7836111e6e8f14126bd0151093d535bb9075b
parent: b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703
author: rodri <rgl@antares-labs.eu>
date: Thu Nov 9 06:29:44 EST 2023
Lesson 0: Bresenham's Line Drawing Algorithm—Fifth attempt
--- a/main.c
+++ b/main.c
@@ -103,8 +103,7 @@
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 @@
}
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;
+ }
}
}