shithub: tinyrend

Download patch

ref: b1f6c2b60fd54e19c21f5cc81e3b47ca18d8f703
parent: 667fd01d924ecc99c1579e155b39cd12fd201f4b
author: rodri <rgl@antares-labs.eu>
date: Thu Nov 9 06:22:35 EST 2023

Lesson 0: Bresenham's Line Drawing Algorithm—Third attempt

--- 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 @@
 }
 
 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;
 
-	dt = 0.01;
+	if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){
+		steep = 1;
+		swap(&p0.x, &p0.y);
+		swap(&p1.x, &p1.y);
+	}
 
-	for(t = 0; t < 1; t += dt){
-		p = Pt(
-			flerp(p0.x, p1.x, t),
-			flerp(p0.y, p1.y, t)
-		);
+	/* 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);
+
 		pixel(dst, p, src);
+
+		if(steep)
+			swap(&p.x, &p.y);
 	}
 }
 
@@ -181,6 +209,8 @@
 	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);
--- a/mkfile
+++ b/mkfile
@@ -1,24 +1,8 @@
 </$objtype/mkfile
 
-BIN=/$objtype/bin/musw
+BIN=/$objtype/bin
 TARG=tinyrend
 OFILES=\
 	main.$O\
 
-HFILES=\
-	./libgeometry/geometry.h\
-
-LIB=\
-	./libgeometry/libgeometry.a$O\
-
-CFLAGS=$CFLAGS
-
 </sys/src/cmd/mkone
-
-./libgeometry/libgeometry.a$O:
-	cd ./libgeometry
-	mk install
-
-clean nuke:V:
-	rm -f *.[$OS] [$OS].??* $TARG
-	@{cd ./libgeometry; mk $target}