correction of centroid

This commit is contained in:
RonaldoCMP 2021-03-30 00:25:29 +01:00
parent fd2c073bb2
commit 575db80056
2 changed files with 19 additions and 18 deletions

View file

@ -1911,23 +1911,24 @@ function align_polygon(reference, poly, angles, cp) =
function centroid(poly, eps=EPSILON) =
assert( is_path(poly,dim=[2,3]), "The input must be a 2D or 3D polygon." )
assert( is_finite(eps) && (eps>=0), "The tolerance should be a non-negative value." )
let(
n = len(poly[0])==2 ? 1 :
let( plane = plane_from_points(poly, fast=true) )
assert( !is_undef(plane), "The polygon must be planar." )
plane_normal(plane),
v0 = poly[0] ,
val = sum([for(i=[1:len(poly)-2])
let(
v1 = poly[i],
v2 = poly[i+1],
area = cross(v2-v0,v1-v0)*n
)
[ area, (v0+v1+v2)*area ]
] )
)
assert(!approx(val[0],0, eps), "The polygon is self-intersecting or its points are collinear.")
val[1]/val[0]/3;
let(
n = len(poly[0])==2 ? 1 :
let(
plane = plane_from_points(poly, fast=true) )
assert( !is_undef(plane), "The polygon must be planar." )
plane_normal(plane),
v0 = poly[0] ,
val = sum([for(i=[1:len(poly)-2])
let(
v1 = poly[i],
v2 = poly[i+1],
area = cross(v2-v0,v1-v0)*n
)
[ area, (v0+v1+v2)*area ]
] )
)
assert(!approx(val[0],0, eps), "The polygon is self-intersecting or its points are collinear.")
val[1]/val[0]/3;

View file

@ -908,7 +908,7 @@ module test_noncollinear_triple() {
module test_centroid() {
$fn = 24;
assert_approx(centroid(circle(d=100)), [0,0]);
assert_approx(centroid(rect([40,60],rounding=10,anchor=LEFT)), [-20,0]);
assert_approx(centroid(rect([40,60],rounding=10,anchor=LEFT)), [-20,0]);
assert_approx(centroid(rect([40,60],rounding=10,anchor=FWD)), [0,30]);
poly = move([1,2.5,3.1],p=rot([12,49,24], p=path3d(circle(10,$fn=33))));
assert_approx(centroid(poly), [1,2.5,3.1]);