summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-11-09 11:29:44 +0000
committerrodri <rgl@antares-labs.eu>2023-11-09 11:29:44 +0000
commit63b7836111e6e8f14126bd0151093d535bb9075b (patch)
tree013b62f2a71c7a45de73334940c2be20fcab72c7
parentb1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703 (diff)
downloadtinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.tar.gz
tinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.tar.bz2
tinyrend-63b7836111e6e8f14126bd0151093d535bb9075b.zip
Lesson 0: Bresenham's Line Drawing Algorithm—Fifth attempt
-rw-r--r--main.c20
1 files changed, 10 insertions, 10 deletions
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;
+ }
}
}