shithub: libgraphics

Download patch

ref: c1a0f17d5272255456af4be4e0aa5a7e697fcf11
parent: afc811ce9e8b8a1a3d83b3855f4a9e63ba5a2c7b
author: rodri <rgl@antares-labs.eu>
date: Fri May 3 11:29:15 EDT 2024

clip: fix rectclipline.

mixed up CLIP[TB] and the slope was being computed as
an integer division, which caused artifacts.

--- a/clip.c
+++ b/clip.c
@@ -179,22 +179,25 @@
 /*
  * Cohen-Sutherland rectangle-line clipping
  */
-void
+int
 rectclipline(Rectangle r, Point *p0, Point *p1)
 {
 	int code0, code1;
-	int Δx;
+	int Δx, Δy;
 	double m;
 
 	Δx = p1->x - p0->x;
-	m = Δx == 0? 0: (p1->y - p0->y)/Δx;
+	Δy = p1->y - p0->y;
+	m = Δx == 0? 0: (double)Δy/Δx;
 
 	for(;;){
 		code0 = outcode(*p0, r);
 		code1 = outcode(*p1, r);
 
-		if(lineisinside(code0, code1) || lineisoutside(code0, code1))
-			break;
+		if(lineisinside(code0, code1))
+			return 0;
+		else if(lineisoutside(code0, code1))
+			return -1;
 
 		if(ptisinside(code0)){
 			swappt(p0, p1);
@@ -207,11 +210,11 @@
 		}else if(code0 & CLIPR){
 			p0->y += (r.max.x - p0->x)*m;
 			p0->x = r.max.x;
-		}else if(code0 & CLIPB){
+		}else if(code0 & CLIPT){
 			if(p0->x != p1->x && m != 0)
 				p0->x += (r.min.y - p0->y)/m;
 			p0->y = r.min.y;
-		}else if(code0 & CLIPT){
+		}else if(code0 & CLIPB){
 			if(p0->x != p1->x && m != 0)
 				p0->x += (r.max.y - p0->y)/m;
 			p0->y = r.max.y;
--- a/internal.h
+++ b/internal.h
@@ -52,7 +52,7 @@
 
 /* clip */
 int clipprimitive(Primitive*);
-void rectclipline(Rectangle, Point*, Point*);
+int rectclipline(Rectangle, Point*, Point*);
 
 /* util */
 int min(int, int);
--- a/render.c
+++ b/render.c
@@ -205,7 +205,8 @@
 		p0 = Pt(prim.v[0].p.x, prim.v[0].p.y);
 		p1 = Pt(prim.v[1].p.x, prim.v[1].p.y);
 		/* clip it against our wr */
-		rectclipline(task->wr, &p0, &p1);
+		if(rectclipline(task->wr, &p0, &p1) < 0)
+			break;
 
 		/* transpose the points */
 		if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){