summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-11-09 11:22:35 +0000
committerrodri <rgl@antares-labs.eu>2023-11-09 11:22:35 +0000
commitb1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703 (patch)
tree8b3f917dc53231556477e065ca8f7069919b542d /main.c
parent667fd01d924ecc99c1579e155b39cd12fd201f4b (diff)
downloadtinyrend-b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703.tar.gz
tinyrend-b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703.tar.bz2
tinyrend-b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703.zip
Lesson 0: Bresenham's Line Drawing Algorithm—Third attempt
Diffstat (limited to 'main.c')
-rw-r--r--main.c48
1 files changed, 39 insertions, 9 deletions
diff --git a/main.c b/main.c
index 0cd9e9a..d6a11cd 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
#include <memdraw.h>
#include <mouse.h>
#include <keyboard.h>
-#include "libgeometry/geometry.h"
+#include <geometry.h>
Memimage *fb;
@@ -91,19 +91,47 @@ pixel(Memimage *dst, Point p, Memimage *src)
}
void
+swap(int *a, int *b)
+{
+ int t;
+
+ t = *a;
+ *a = *b;
+ *b = t;
+}
+
+void
bresenham(Memimage *dst, Point p0, Point p1, Memimage *src)
{
- double t, dt;
- Point p;
+ int steep = 0;
+ double t;
+ Point p, dp;
+
+ if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){
+ steep = 1;
+ swap(&p0.x, &p0.y);
+ swap(&p1.x, &p1.y);
+ }
- dt = 0.01;
+ /* make them left-to-right */
+ if(p0.x > p1.x){
+ swap(&p0.x, &p1.x);
+ swap(&p0.y, &p1.y);
+ }
+
+ dp = subpt(p1, p0);
+
+ 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);
- for(t = 0; t < 1; t += dt){
- p = Pt(
- flerp(p0.x, p1.x, t),
- flerp(p0.y, p1.y, t)
- );
pixel(dst, p, src);
+
+ if(steep)
+ swap(&p.x, &p.y);
}
}
@@ -181,6 +209,8 @@ threadmain(int argc, char *argv[])
fb = eallocmemimage(screen->r, screen->chan);
red = rgb(DRed);
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);
display->locking = 1;
unlockdisplay(display);