diff --git a/geometry.scad b/geometry.scad index 30e1d9d..7cda463 100644 --- a/geometry.scad +++ b/geometry.scad @@ -1689,9 +1689,10 @@ function furthest_point(pt, points) = // area = polygon_area(poly); // Topics: Geometry, Polygons, Area // Description: -// Given a 2D or 3D planar polygon, returns the area of that polygon. -// If the polygon is self-crossing, the results are undefined. For non-planar 3D polygon the result is `undef`. -// When `signed` is true, a signed area is returned; a positive area indicates a clockwise polygon. +// Given a 2D or 3D simple planar polygon, returns the area of that polygon. +// If the polygon is self-intersecting or non-planar, the result is `undef.` +// When `signed` is true and the polygon is 2d, a signed area is returned: a positive area indicates a counter-clockwise polygon. +// The area of 3d polygons is always nonnegative. // Arguments: // poly = Polygon to compute the area of. // signed = If true, a signed area is returned. Default: false. @@ -1706,7 +1707,7 @@ function polygon_area(poly, signed=false) = let( n = plane_normal(plane), total = - sum([ for(i=[1:1:len(poly)-2]) + -sum([ for(i=[1:1:len(poly)-2]) cross(poly[i]-poly[0], poly[i+1]-poly[0]) ]) * n/2 ) diff --git a/tests/test_geometry.scad b/tests/test_geometry.scad index 19182c4..2c19e6d 100644 --- a/tests/test_geometry.scad +++ b/tests/test_geometry.scad @@ -32,7 +32,6 @@ test_tri_calc(); //test_hyp_adj_to_ang(); //test_hyp_opp_to_ang(); //test_adj_opp_to_ang(); -test_triangle_area(); test_plane3pt(); test_plane3pt_indexed(); test_plane_from_normal(); @@ -649,13 +648,6 @@ module test_hyp_opp_to_ang() nil(); // Covered in test_tri_functions() module test_adj_opp_to_ang() nil(); // Covered in test_tri_functions() -module test_triangle_area() { - assert(abs(triangle_area([0,0], [0,10], [10,0]) + 50) < EPSILON); - assert(abs(triangle_area([0,0], [0,10], [0,15])) < EPSILON); - assert(abs(triangle_area([0,0], [10,0], [0,10]) - 50) < EPSILON); -} -*test_triangle_area(); - module test_plane3pt() { assert_approx(plane3pt([0,0,20], [0,10,10], [0,0,0]), [1,0,0,0]); @@ -817,6 +809,10 @@ module test_polygon_area() { assert(approx(polygon_area(rot([13,27,75], p=path3d(circle(r=50,$fn=1000),fill=23)), signed=true), -PI*50*50, eps=0.1)); + assert(abs(triangle_area([0,0], [0,10], [10,0]) + 50) < EPSILON); + assert(abs(triangle_area([0,0], [0,10], [0,15])) < EPSILON); + assert(abs(triangle_area([0,0], [10,0], [0,10]) - 50) < EPSILON); + } *test_polygon_area();