diff --git a/docs/images/lib-path_extend-1.JPG b/docs/images/lib-path_extend-1.JPG new file mode 100644 index 00000000..77710e45 Binary files /dev/null and b/docs/images/lib-path_extend-1.JPG differ diff --git a/docs/lib-path_extend.md b/docs/lib-path_extend.md new file mode 100644 index 00000000..49f7caf5 --- /dev/null +++ b/docs/lib-path_extend.md @@ -0,0 +1,42 @@ +# path_extend + +It extends a 2D stroke along a path. This module is suitable for a path created by a continuous function. + +It depends on the rotate_p function and the polytransversals module. Remember to include "rotate_p.scad" and "polytransversals.scad". + +When using this module, you should use points to represent the 2D stroke. + +## Parameters + +- `stroke_pts` : A list of points represent a stroke. See the example below. +- `path_pts` : A list of points represent the path. + +## Examples + + include ; + include ; + include ; + include ; + include ; + + $fn = 96; + + stroke1 = [[-5, 2.5], [-2.5, 0], [0, 2.5], [2.5, 0], [5, 2.5]]; + path_pts1 = circle_path(50, 60); + path_extend(stroke1, path_pts1); + + stroke2 = [[-4, 0], [0, 4], [4, 0]]; + pts_angles = archimedean_spiral( + arm_distance = 17, + init_angle = 180, + point_distance = 5, + num_of_points = 85 + ); + + translate([120, 0, 0]) + path_extend( + stroke2, + [for(pa = pts_angles) pa[0]] + ); + +![path_extend](images/lib-path_extend-1.JPG) \ No newline at end of file diff --git a/src/path_extend.scad b/src/path_extend.scad new file mode 100644 index 00000000..1f2cc7e3 --- /dev/null +++ b/src/path_extend.scad @@ -0,0 +1,66 @@ +/** +* path_extend.scad +* +* It extends a 2D stroke along a path. +* This module is suitable for a path created by a continuous function. +* It depends on the rotate_p function and the polytransversals module. +* Remember to include "rotate_p.scad" and "polytransversals.scad". +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib-path_extend.html +* +**/ + +module path_extend(stroke_pts, path_pts) { + function length(p1, p2) = + let( + x1 = p1[0], + y1 = p1[1], + x2 = p2[0], + y2 = p2[1] + ) sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); + + function az(p1, p2) = + let( + x1 = p1[0], + y1 = p1[1], + x2 = p2[0], + y2 = p2[1] + ) -90 + atan2((y2 - y1), (x2 - x1)); + + leng_path_pts = len(path_pts); + + function first_stroke() = + let( + p1 = path_pts[0], + p2 = path_pts[1], + a = az(p1, p2) + ) + [ + for(p = stroke_pts) + rotate_p(p, a) + p1 + ]; + + function stroke(p1, p2) = + let( + leng = length(p1, p2), + a = az(p1, p2) + ) + [ + for(p = stroke_pts) + rotate_p(p + [0, leng], a) + p1 + ]; + + function path_extend_inner(index) = + index == leng_path_pts ? [] : + concat( + [stroke(path_pts[index - 1], path_pts[index])], + path_extend_inner(index + 1) + ); + + polytransversals( + concat([first_stroke()], path_extend_inner(1)) + ); +}