git: 9front

Download patch

ref: c0421231374a55b38075f98f0ebf4afcd338d49a
parent: 6d4887cafd1a3f7f84f8df23974a0e9b6673603f
author: rodri <rgl@antares-labs.eu>
date: Sat Mar 14 14:43:45 EDT 2026

libgeometry: make centroid3?() right

don't restrict it to triangles only.

--- a/sys/include/geometry.h
+++ b/sys/include/geometry.h
@@ -53,7 +53,7 @@
 double dotvec2(Point2, Point2);
 double vec2len(Point2);
 Point2 normvec2(Point2);
-Point2 centroid(Point2, Point2, Point2);
+Point2 centroid(Point2*, ulong);
 Point3 barycoords(Point2, Point2, Point2, Point2);
 int edgeptcmp(Point2, Point2, Point2);
 int ptinpoly(Point2, Point2*, ulong);
@@ -71,7 +71,7 @@
 Point3 crossvec3(Point3, Point3);
 double vec3len(Point3);
 Point3 normvec3(Point3);
-Point3 centroid3(Point3, Point3, Point3);
+Point3 centroid3(Point3*, ulong);
 int lineXsphere(Point3*, Point3, Point3, Point3, double, int);
 int ptincylinder(Point3, Point3, Point3, double);
 int ptincone(Point3, Point3, Point3, double);
--- a/sys/man/2/geometry
+++ b/sys/man/2/geometry
@@ -64,7 +64,7 @@
 double dotvec2(Point2 a, Point2 b)
 double vec2len(Point2 v)
 Point2 normvec2(Point2 v)
-Point2 centroid(Point2 p0, Point2 p1, Point2 p2)
+Point2 centroid(Point2 *pts, ulong npts)
 Point3 barycoords(Point2 p0, Point2 p1, Point2 p2, Point2 p)
 int edgeptcmp(Point2 e0, Point2 e1, Point2 p)
 int ptinpoly(Point2 p, Point2 *pts, ulong npts)
@@ -82,7 +82,7 @@
 Point3 crossvec3(Point3 a, Point3 b)
 double vec3len(Point3 v)
 Point3 normvec3(Point3 v)
-Point3 centroid3(Point3 p0, Point3 p1, Point3 p2)
+Point3 centroid3(Point3 *pts, ulong npts)
 int lineXsphere(Point3 *rp, Point3 p0, Point3 p1,
 		Point3 c, double rad, int isaray)
 int ptincylinder(Point3 p, Point3 p0, Point3 p1, double r)
@@ -270,8 +270,8 @@
 .I v
 and returns a new 2D point.
 .TP
-.B centroid(\fIp0\fP,\fIp1\fP,\fIp2\fP)
-Returns the geometric center of a triangle defined by three points.
+.B centroid(\fIpts\fP,\fInpts\fP)
+Returns the geometric center of a collection of 2D points.
 .TP
 .B barycoords(\fIp0\fP,\fIp1\fP,\fIp2\fP,\fIp\fP)
 Returns a 3D point that represents the barycentric coordinates of the
@@ -370,8 +370,8 @@
 .I v
 and returns a new 3D point.
 .TP
-.B centroid3(\fIp0\fP,\fIp1\fP,\fIp2\fP)
-Returns the geometric center of a triangle defined by three points.
+.B centroid3(\fIpts\fP,\fInpts\fP)
+Returns the geometric center of a collection of 3D points.
 .TP
 .B lineXsphere(\fIrp\fP,\fIp0\fP,\fIp1\fP,\fIc\fP,\fIrad\fP,\fIisaray\fP)
 Finds the intersection between the line defined by
--- a/sys/src/libgeometry/point.c
+++ b/sys/src/libgeometry/point.c
@@ -87,9 +87,15 @@
 }
 
 Point2
-centroid(Point2 p0, Point2 p1, Point2 p2)
+centroid(Point2 *pts, ulong npts)
 {
-	return divpt2(addpt2(p0, addpt2(p1, p2)), 3);
+	Point2 p;
+	ulong i;
+
+	p = pts[0];
+	for(i = 1; i < npts; i++)
+		p = addpt2(p, pts[i]);
+	return divpt2(p, npts);
 }
 
 /*
@@ -248,9 +254,15 @@
 }
 
 Point3
-centroid3(Point3 p0, Point3 p1, Point3 p2)
+centroid3(Point3 *pts, ulong npts)
 {
-	return divpt3(addpt3(p0, addpt3(p1, p2)), 3);
+	Point3 p;
+	ulong i;
+
+	p = pts[0];
+	for(i = 1; i < npts; i++)
+		p = addpt3(p, pts[i]);
+	return divpt3(p, npts);
 }
 
 int
--