triangle_area2d() -> triangle_area(). Added polygon_area()

This commit is contained in:
Revar Desmera 2019-07-10 13:37:23 -07:00
parent e00ce0d81f
commit 9f0d55f5b3
2 changed files with 19 additions and 10 deletions

View file

@ -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);

View file

@ -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
];