From aedca84aa2f7bee7e6d2838c7137411facbbdce6 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 19 May 2017 11:14:09 +0800 Subject: [PATCH] auto-calculating angles when angles is undef --- src/along_with.scad | 47 +++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/along_with.scad b/src/along_with.scad index 3e3e3e89..d685e810 100644 --- a/src/along_with.scad +++ b/src/along_with.scad @@ -11,25 +11,48 @@ * **/ +include <__private__/__angy_angz.scad>; + module along_with(points, angles) { - module rotOrNot(i) { - if(angles == undef) { - children(0); - } else { - rotate(angles[i]) - children(0); - } - } + function _path_angles(path_pts, leng_path_pts, i = 0) = + i == leng_path_pts - 1 ? + [] : + concat( + [__angy_angz(path_pts[i], path_pts[i + 1])], + _path_angles(path_pts, leng_path_pts, i + 1) + ); + + function path_angles(path_pts) = + let( + leng_path_pts = len(path_pts), + angs = _path_angles(path_pts, leng_path_pts) + ) + concat( + [[0, -angs[0][0], angs[0][1]]], + [for(a = angs) [0, -a[0], a[1]]] + ); + + angles_defined = angles != undef; + angs = angles_defined ? angles : path_angles(path_pts); + + module align(i) { + translate(points[i]) + rotate(angs[i]) + if(angles_defined) { + children(0); + } else { + rotate([90, 0, -90]) + children(0); + } + } if($children == 1) { for(i = [0:len(points) - 1]) { - translate(points[i]) - rotOrNot(i) children(0); + align(i) children(0); } } else { for(i = [0:min(len(points), $children) - 1]) { - translate(points[i]) - rotOrNot(i) children(i); + align(i) children(i); } } } \ No newline at end of file