diff --git a/geometry.scad b/geometry.scad index a2b10bb..c8bb2a3 100644 --- a/geometry.scad +++ b/geometry.scad @@ -1911,24 +1911,23 @@ 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; diff --git a/tests/test_geometry.scad b/tests/test_geometry.scad index 6133ead..f320390 100644 --- a/tests/test_geometry.scad +++ b/tests/test_geometry.scad @@ -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]);