shithub: libgraphics

Download patch

ref: a68452b76fdc6b319857e8b5a9a9f68063c63299
parent: 8f076c6ee4f318dd8b32daa33cac9ed7985fa11a
author: rodri <rgl@antares-labs.eu>
date: Mon Jun 30 10:12:32 EDT 2025

clip: take _adjustlineverts() out of _rectclipline()

--- a/clip.c
+++ b/clip.c
@@ -183,12 +183,12 @@
 }
 
 /* lerp vertex attributes to match the new positions */
-static void
-adjustverts(Point *p0, Point *p1, BVertex *v0, BVertex *v1)
+void
+_adjustlineverts(Point *p0, Point *p1, BVertex *v0, BVertex *v1)
 {
 	BVertex v[2];
 	Point3 dp;
-	Point Δp;
+	Point v0p, Δp;
 	double len, perc;
 
 	memset(v, 0, sizeof v);
@@ -195,12 +195,13 @@
 
 	dp = subpt3(v1->p, v0->p);
 	len = hypot(dp.x, dp.y);
+	v0p = (Point){v0->p.x, v0->p.y};
 
-	Δp = subpt((Point){v0->p.x, v0->p.y}, *p0);
+	Δp = subpt(v0p, *p0);
 	perc = len == 0? 0: hypot(Δp.x, Δp.y)/len;
 	_lerpvertex(&v[0], v0, v1, perc);
 
-	Δp = subpt((Point){v0->p.x, v0->p.y}, *p1);
+	Δp = subpt(v0p, *p1);
 	perc = len == 0? 0: hypot(Δp.x, Δp.y)/len;
 	_lerpvertex(&v[1], v0, v1, perc);
 
@@ -214,7 +215,7 @@
  * Cohen-Sutherland rectangle-line clipping
  */
 int
-_rectclipline(Rectangle r, Point *p0, Point *p1, BVertex *v0, BVertex *v1)
+_rectclipline(Rectangle r, Point *p0, Point *p1)
 {
 	int code0, code1;
 	int Δx, Δy;
@@ -228,16 +229,14 @@
 		code0 = outcode(*p0, r);
 		code1 = outcode(*p1, r);
 
-		if(lineisinside(code0, code1)){
-			adjustverts(p0, p1, v0, v1);
+		if(lineisinside(code0, code1))
 			return 0;
-		}else if(lineisoutside(code0, code1))
+		else if(lineisoutside(code0, code1))
 			return -1;
 
 		if(ptisinside(code0)){
 			SWAP(Point, p0, p1);
 			SWAP(int, &code0, &code1);
-			SWAP(BVertex, v0, v1);
 		}
 
 		if(code0 & CLIPL){
--- a/internal.h
+++ b/internal.h
@@ -124,7 +124,8 @@
 
 /* clip */
 int _clipprimitive(BPrimitive*, BPrimitive*);
-int _rectclipline(Rectangle, Point*, Point*, BVertex*, BVertex*);
+void _adjustlineverts(Point*, Point*, BVertex*, BVertex*);
+int _rectclipline(Rectangle, Point*, Point*);
 
 /* util */
 void _memsetl(void*, ulong, usize);
--- a/render.c
+++ b/render.c
@@ -237,8 +237,10 @@
 	p0 = (Point){prim->v[0].p.x, prim->v[0].p.y};
 	p1 = (Point){prim->v[1].p.x, prim->v[1].p.y};
 	/* clip it against our wr */
-	if(_rectclipline(task->wr, &p0, &p1, prim->v+0, prim->v+1) < 0)
+	if(_rectclipline(task->wr, &p0, &p1) < 0)
 		return;
+
+	_adjustlineverts(&p0, &p1, prim->v+0, prim->v+1);
 
 	steep = 0;
 	/* transpose the points */
--