From bb92d788eff757279da496409bd5a06b857d56dd Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Sun, 17 Nov 2019 18:19:55 -0800 Subject: [PATCH] Added reverse_polygon() and improved ccw_polygon() and clockwise_polygon() --- geometry.scad | 13 +++++++++++-- tests/test_geometry.scad | 17 ++++++++++++++--- version.scad | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/geometry.scad b/geometry.scad index c4eb861..1504eea 100644 --- a/geometry.scad +++ b/geometry.scad @@ -1230,7 +1230,7 @@ function polygon_is_clockwise(path) = // Description: // Given a polygon path, returns the clockwise winding version of that path. function clockwise_polygon(path) = - polygon_is_clockwise(path)? path : reverse(path); + polygon_is_clockwise(path)? path : reverse_polygon(path); // Function: ccw_polygon() @@ -1239,7 +1239,16 @@ function clockwise_polygon(path) = // Description: // Given a polygon path, returns the counter-clockwise winding version of that path. function ccw_polygon(path) = - polygon_is_clockwise(path)? reverse(path) : path; + polygon_is_clockwise(path)? reverse_polygon(path) : path; + + +// Function: reverse_polygon() +// Usage: +// reverse_polygon(poly) +// Description: +// Reverses a polygon's winding direction, while still using the same start point. +function reverse_polygon(poly) = + let(lp=len(poly)) [for (i=idx(poly)) poly[(lp-i)%lp]]; diff --git a/tests/test_geometry.scad b/tests/test_geometry.scad index c762503..2b8f3ce 100644 --- a/tests/test_geometry.scad +++ b/tests/test_geometry.scad @@ -655,20 +655,31 @@ test_polygon_is_clockwise(); module test_clockwise_polygon() { path = circle(d=100); + rpath = concat([path[0]], reverse(select(path,1,-1))); assert(clockwise_polygon(path) == path); - assert(clockwise_polygon(reverse(path)) == path); + assert(clockwise_polygon(rpath) == path); } test_clockwise_polygon(); module test_ccw_polygon() { path = circle(d=100); - assert(ccw_polygon(path) == reverse(path)); - assert(ccw_polygon(reverse(path)) == reverse(path)); + rpath = concat([path[0]], reverse(select(path,1,-1))); + assert(ccw_polygon(path) == rpath); + assert(ccw_polygon(rpath) == rpath); } test_ccw_polygon(); +module test_reverse_polygon() { + path = circle(d=100); + rpath = concat([path[0]], reverse(select(path,1,-1))); + assert(reverse_polygon(path) == rpath); + assert(reverse_polygon(rpath) == path); +} +test_reverse_polygon(); + + module test_is_region() { assert(is_region([circle(d=10),square(10)])); assert(is_region([circle(d=10),square(10),circle(d=50)])); diff --git a/version.scad b/version.scad index ede42f6..f418189 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,41]; +BOSL_VERSION = [2,0,42]; // Section: BOSL Library Version Functions