diff options
author | rodri <rgl@antares-labs.eu> | 2023-11-09 11:29:44 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-11-09 11:29:44 +0000 |
commit | 63b7836111e6e8f14126bd0151093d535bb9075b (patch) | |
tree | 013b62f2a71c7a45de73334940c2be20fcab72c7 | |
parent | b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703 (diff) | |
download | tinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.tar.gz tinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.tar.bz2 tinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.zip |
Lesson 0: Bresenham's Line Drawing Algorithm—Fifth attempt
-rw-r--r-- | main.c | 20 |
1 files changed, 10 insertions, 10 deletions
@@ -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; + } } } |