From 84bd2088e53adf8ab4c2956067baa0d54d06d552 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Sat, 20 Mar 2021 01:37:46 -0700 Subject: [PATCH] Optimized cleanup_path() and list_rotate() --- arrays.scad | 9 ++++++++- paths.scad | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/arrays.scad b/arrays.scad index 7c420b5..3fd8bbc 100644 --- a/arrays.scad +++ b/arrays.scad @@ -432,7 +432,14 @@ function reverse(x) = function list_rotate(list,n=1) = assert(is_list(list)||is_string(list), "Invalid list or string.") assert(is_finite(n), "Invalid number") - let (elems = select(list,n,n+len(list)-1)) + let ( + ll = len(list), + n = ((n % ll) + ll) % ll, + elems = [ + for (i=[n:1:ll-1]) list[i], + for (i=[0:1:n-1]) list[i] + ] + ) is_string(list)? str_join(elems) : elems; diff --git a/paths.scad b/paths.scad index 06a5f7e..5d391c3 100644 --- a/paths.scad +++ b/paths.scad @@ -62,7 +62,8 @@ function is_closed_path(path, eps=EPSILON) = approx(path[0], path[len(path)-1], // close_path(path); // Description: // If a path's last point does not coincide with its first point, closes the path so it does. -function close_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? path : concat(path,[path[0]]); +function close_path(path, eps=EPSILON) = + is_closed_path(path,eps=eps)? path : concat(path,[path[0]]); // Function: cleanup_path() @@ -70,7 +71,8 @@ function close_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? path : co // cleanup_path(path); // Description: // If a path's last point coincides with its first point, deletes the last point in the path. -function cleanup_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? select(path,0,-2) : path; +function cleanup_path(path, eps=EPSILON) = + is_closed_path(path,eps=eps)? [for (i=[0:1:len(path)-2]) path[i]] : path; // Function: path_subselect()