diff --git a/README.md b/README.md index 4369d95c..6e2d22e2 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Too many dependencies? Because OpenSCAD doesn't provide namespace management, I - [shape_cyclicpolygon](https://openhome.cc/eGossip/OpenSCAD/lib-shape_cyclicpolygon.html) - [shape_pentagram](https://openhome.cc/eGossip/OpenSCAD/lib-shape_pentagram.html) - [shape_superformula](https://openhome.cc/eGossip/OpenSCAD/lib-shape_superformula.html) + - [shape_glued2circles](https://openhome.cc/eGossip/OpenSCAD/lib-shape_glued2circles.html) - [shape_path_extend](https://openhome.cc/eGossip/OpenSCAD/lib-shape_path_extend.html) - 2D Shape Extrusion diff --git a/docs/images/lib-shape_glued2circles-1.JPG b/docs/images/lib-shape_glued2circles-1.JPG new file mode 100644 index 00000000..25de4a77 Binary files /dev/null and b/docs/images/lib-shape_glued2circles-1.JPG differ diff --git a/docs/images/lib-shape_glued2circles-2.JPG b/docs/images/lib-shape_glued2circles-2.JPG new file mode 100644 index 00000000..850802f5 Binary files /dev/null and b/docs/images/lib-shape_glued2circles-2.JPG differ diff --git a/docs/images/lib-shape_glued2circles-3.JPG b/docs/images/lib-shape_glued2circles-3.JPG new file mode 100644 index 00000000..0eea1e0a Binary files /dev/null and b/docs/images/lib-shape_glued2circles-3.JPG differ diff --git a/docs/lib-shape_glued2circles.md b/docs/lib-shape_glued2circles.md new file mode 100644 index 00000000..c9aee833 --- /dev/null +++ b/docs/lib-shape_glued2circles.md @@ -0,0 +1,75 @@ +# shape_glued2circles + +Returns shape points of two glued circles. They can be used with xxx_extrude modules of dotSCAD. The shape points can be also used with the built-in polygon module. + +Dependencies: `rotate_p`, `bezier_curve`, `shape_pie`. + +## Parameters + +- `radius` : The radius of two circles. +- `centre_dist` : The distance between centres of two circles. +- `tangent_angle` : The angle of a tangent line. It defaults to 30 degrees. See examples below. +- `t_step` : It defaults to 0.1. See [bezier_curve](https://openhome.cc/eGossip/OpenSCAD/lib-bezier_curve.html) for details. +- `$fa`, `$fs`, `$fn` : Check [the circle module](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#circle) for more details. + +## Examples + + include ; + include ; + include ; + include ; + + $fn = 36; + + radius = 10; + centre_dist = 30; + + shape_pts = shape_glued2circles(radius, centre_dist); + polygon(shape_pts); + +![shape_glued2circles](images/lib-shape_glued2circles-1.JPG) + + include ; + include ; + include ; + include ; + + $fn = 36; + + radius = 10; + centre_dist = 30; + + shape_pts = shape_glued2circles(radius, centre_dist); + width = centre_dist / 2 + radius; + + rotate_extrude() difference() { + polygon(shape_pts); + + translate([-width, -radius]) + square([width, radius * 2]); + } + +![shape_glued2circles](images/lib-shape_glued2circles-2.JPG) + + include ; + include ; + include ; + include ; + + $fn = 36; + + radius = 10; + centre_dist = 30; + + shape_pts = shape_glued2circles(radius, centre_dist); + width = centre_dist + radius * 2; + + rotate_extrude() translate([0.00001, 0, 0]) + rotate(-90) difference() { + polygon(shape_pts); + + translate([0, -radius / 2]) + square([width, radius], center = true); + } + +![shape_glued2circles](images/lib-shape_glued2circles-3.JPG) \ No newline at end of file diff --git a/src/shape_glued2circles.scad b/src/shape_glued2circles.scad new file mode 100644 index 00000000..5f6ae9c6 --- /dev/null +++ b/src/shape_glued2circles.scad @@ -0,0 +1,79 @@ + +/** +* shape_glued2circles.scad +* +* Returns shape points of two glued circles. +* They can be used with xxx_extrude modules of dotSCAD. +* The shape points can be also used with the built-in polygon module. +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib-shape_glued2circles.html +* +**/ + +function _glued2circles_pie_curve(radius, centre_dist, tangent_angle) = + let( + begin_ang = 90 + tangent_angle, + shape_pts = shape_pie(radius, [-begin_ang, begin_ang]) + ) + [ + for(i = [1:len(shape_pts) - 1]) + shape_pts[i] + [centre_dist / 2, 0] + ]; + +function _glued2circles_bezier(radius, centre_dist, tangent_angle, t_step, ctrl_p1) = + let( + ctrl_p = rotate_p([radius * tan(tangent_angle), -radius], tangent_angle), + ctrl_p2 = [-ctrl_p[0], ctrl_p[1]] + [centre_dist / 2, 0], + ctrl_p3 = [-ctrl_p2[0], ctrl_p2[1]], + ctrl_p4 = [-ctrl_p1[0], ctrl_p1[1]] + ) + bezier_curve( + t_step, + [ + ctrl_p1, + ctrl_p2, + ctrl_p3, + ctrl_p4 + ] + ); + +function _glued2circles_lower_half_curve(curve_pts, leng, i = 0) = + i < leng ? ( + curve_pts[leng - 1 - i][0] >= 0 ? + concat( + [curve_pts[leng - 1 - i]], + _glued2circles_lower_half_curve(curve_pts, leng, i + 1) + ) : + _glued2circles_lower_half_curve(curve_pts, leng, i + 1) + ) : []; + +function _glued2circles_half_glued_circle(radius, centre_dist, tangent_angle, t_step) = + let( + pie_curve_pts = _glued2circles_pie_curve(radius, centre_dist, tangent_angle), + curve_pts = _glued2circles_bezier(radius, centre_dist, tangent_angle, t_step, pie_curve_pts[0]), + lower_curve_pts = _glued2circles_lower_half_curve(curve_pts, len(curve_pts)), + leng_half_curve_pts = len(lower_curve_pts), + upper_curve_pts = [ + for(i = [0:leng_half_curve_pts - 1]) + let(pt = lower_curve_pts[leng_half_curve_pts - 1 - i]) + [pt[0], -pt[1]] + ] + ) concat( + lower_curve_pts, + pie_curve_pts, + upper_curve_pts + ); + +function shape_glued2circles(radius, centre_dist, tangent_angle = 30, t_step = 0.1) = + let( + half_glued_circles = _glued2circles_half_glued_circle(radius, centre_dist, tangent_angle, t_step), + leng_half_glued_circles = len(half_glued_circles), + left_half_glued_circles = [ + for(i = [0:leng_half_glued_circles - 1]) + let(pt = half_glued_circles[leng_half_glued_circles - 1 - i]) + [-pt[0], pt[1]] + ] + ) concat(half_glued_circles, left_half_glued_circles); \ No newline at end of file