diff --git a/geometry.scad b/geometry.scad index 09a3ade..55bc01f 100644 --- a/geometry.scad +++ b/geometry.scad @@ -207,17 +207,17 @@ function find_circle_2tangents(pt1, pt2, pt3, r=undef, d=undef) = ) [cp, n]; -// Function: triangle_area2d() +// Function: triangle_area() // Usage: -// triangle_area2d(a,b,c); +// triangle_area(a,b,c); // Description: -// Returns the area of a triangle formed between three vertices. -// Result will be negative if the points are in clockwise order. +// Returns the area of a triangle formed between three 2D or 3D vertices. +// Result will be negative if the points are 2D and in in clockwise order. // Examples: -// triangle_area2d([0,0], [5,10], [10,0]); // Returns -50 -// triangle_area2d([10,0], [5,10], [0,0]); // Returns 50 -function triangle_area2d(a,b,c) = - ( +// triangle_area([0,0], [5,10], [10,0]); // Returns -50 +// triangle_area([10,0], [5,10], [0,0]); // Returns 50 +function triangle_area(a,b,c) = + len(a)==3? 0.5*norm(cross(c-a,c-b)) : ( a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) @@ -376,6 +376,15 @@ function path_subselect(path,s1,u1,s2,u2) = ) pathout; +// Function: polygon_area() +// Usage: +// area = polygon_area(vertices); +// Description: +// Given a polygon, returns the area of that polygon. If the polygon is self-crossing, the results are undefined. +function polygon_area(vertices) = + 0.5*sum([for(i=[0:len(vertices)-1]) det2(select(vertices,i,i+1))]); + + // Function: assemble_path_fragments() // Usage: // assemble_path_fragments(subpaths); diff --git a/hull.scad b/hull.scad index a483dc7..1cfa69d 100644 --- a/hull.scad +++ b/hull.scad @@ -91,7 +91,7 @@ function hull2d_path(points) = c = _find_first_noncollinear([a,b], points, 2) ) (c == len(points))? _hull2d_collinear(points) : let( remaining = [ for (i = [2:1:len(points)-1]) if (i != c) i ], - ccw = triangle_area2d(points[a], points[b], points[c]) > 0, + ccw = triangle_area(points[a], points[b], points[c]) > 0, polygon = ccw? [a,b,c] : [a,c,b] ) _hull2d_iterative(points, polygon, remaining); @@ -131,7 +131,7 @@ function _find_conflicting_segments(points, polygon, point) = [ j = (i+1) % len(polygon), p1 = points[polygon[i]], p2 = points[polygon[j]], - area = triangle_area2d(p1, p2, point) + area = triangle_area(p1, p2, point) ) if (area < 0) i ];