diff --git a/README.md b/README.md index 9922abd1..2a6d3d38 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Too many dependencies? Because OpenSCAD doesn't provide namespace management, I - [shape_ellipse](https://openhome.cc/eGossip/OpenSCAD/lib-shape_ellipse.html) - [shape_square](https://openhome.cc/eGossip/OpenSCAD/lib-shape_square.html) - [shape_trapezium](https://openhome.cc/eGossip/OpenSCAD/lib-shape_trapezium.html) + - [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) diff --git a/docs/images/lib-shape_cyclicpolygon-1.JPG b/docs/images/lib-shape_cyclicpolygon-1.JPG new file mode 100644 index 00000000..c2ce3034 Binary files /dev/null and b/docs/images/lib-shape_cyclicpolygon-1.JPG differ diff --git a/docs/images/lib-shape_cyclicpolygon-2.JPG b/docs/images/lib-shape_cyclicpolygon-2.JPG new file mode 100644 index 00000000..69084f29 Binary files /dev/null and b/docs/images/lib-shape_cyclicpolygon-2.JPG differ diff --git a/docs/lib-shape_cyclicpolygon.md b/docs/lib-shape_cyclicpolygon.md new file mode 100644 index 00000000..07f7780a --- /dev/null +++ b/docs/lib-shape_cyclicpolygon.md @@ -0,0 +1,58 @@ +# shape_cyclicpolygon + +Returns shape points of a regular cyclic polygon. They can be used with xxx_extrude modules of dotSCAD. The shape points can be also used with the built-in polygon module. + +## Parameters + +- `sides` : The radius of the circle. +- `circle_r` : The radius of the circumcircle. +- `$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 ; + + circle_r = 10; + corner_r = 3; + + $fn = 24; + + for(i = [0:2]) { + translate([i * circle_r * 2, 0, 0]) + polygon( + shape_cyclicpolygon( + sides = 3 + i, + circle_r = circle_r, + corner_r = corner_r + ) + ); + } + + for(i = [0:2]) { + translate([i * circle_r * 2, -circle_r * 2, 0]) + polygon( + shape_cyclicpolygon( + sides = 4 + i, + circle_r = circle_r , + corner_r = corner_r + ) + ); + } + +![shape_cyclicpolygon](images/lib-shape_cyclicpolygon-1.JPG) + + include ; + include ; + include ; + include ; + include ; + + shape_pts = shape_cyclicpolygon( + sides = 5, + circle_r = 10, + corner_r = 3 + ); + + ring_extrude(shape_pts, radius = 20, angle = 180, twist = 90); + +![shape_cyclicpolygon](images/lib-shape_cyclicpolygon-2.JPG) diff --git a/src/shape_cyclicpolygon.scad b/src/shape_cyclicpolygon.scad new file mode 100644 index 00000000..defa0678 --- /dev/null +++ b/src/shape_cyclicpolygon.scad @@ -0,0 +1,55 @@ +/** +* shape_cyclicpolygon.scad +* +* Returns shape points of a regular cyclic polygon. +* 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_cyclicpolygon.html +* +**/ + +include <__private__/__frags.scad>; +include <__private__/__pie_for_rounding.scad>; + +function shape_cyclicpolygon(sides, circle_r, corner_r) = + let( + frag_a = 360 / sides, + corner_a = (180 - frag_a), + corner_circle_a = 180 - corner_a, + half_corner_circle_a = corner_circle_a / 2, + corner_circle_center = circle_r - corner_r / sin(corner_a / 2), + first_corner = [ + for( + pt = __pie_for_rounding( + corner_r, + -half_corner_circle_a, + half_corner_circle_a, + __frags(corner_r) * corner_circle_a / 360 + ) + ) + [pt[0] + corner_circle_center, pt[1]] + ] + + ) + concat( + first_corner, + [ + for(side = [1:sides - 1]) + for(pt = first_corner) + let( + a = frag_a * side, + x = pt[0], + y = pt[1], + sina = sin(a), + cosa = cos(a) + ) + [ + x * cosa - y * sina, + x * sina + y * cosa + ] + ] + ); \ No newline at end of file