From faf12b6e26cccb83bc5c1e816cae9ebfd2ece6d6 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Sun, 3 Nov 2019 18:44:14 -0800 Subject: [PATCH] Added circum= and realign= args to circle() --- primitives.scad | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/primitives.scad b/primitives.scad index 3c49d42..ae3dbba 100644 --- a/primitives.scad +++ b/primitives.scad @@ -55,11 +55,13 @@ function square(size=1, center=undef, anchor=FRONT+LEFT, spin=0) = // Usage: // circle(r|d, [anchor]) // Description: -// When called as a module, creates a 2D circle of the given size. -// When called as a function, returns a 2D path/list of points for a circle of the given size. +// When called as a module, creates a 2D polygon that approximates a circle of the given size. +// When called as a function, returns a 2D list of points (path) for a polygon that approximates a circle of the given size. // Arguments: // r = The radius of the circle to create. // d = The diameter of the circle to create. +// realign = If true, rotates the polygon that approximates the circle by half of one size. +// circum = If true, the polygon that approximates the circle will be upsized slightly to circumscribe the theoretical circle. If false, it inscribes the theoretical circle. Default: false // anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER` // spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0` // Example(2D): By Radius @@ -72,23 +74,26 @@ function square(size=1, center=undef, anchor=FRONT+LEFT, spin=0) = // circle(d=50, anchor=FRONT, spin=45); // Example(NORENDER): Called as Function // path = circle(d=50, anchor=FRONT, spin=45); -module circle(r=undef, d=undef, anchor=CENTER, spin=0) { +module circle(r=undef, d=undef, realign=false, circum=false, anchor=CENTER, spin=0) { r = get_radius(r=r, d=d, dflt=1); sides = segs(r); - pts = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]]; - orient_and_anchor([2*r,2*r,0], UP, anchor, spin=spin, geometry="cylinder", two_d=true, chain=true) { + rr = circum? r/cos(180/sides) : r; + pts = circle(r=rr, realign=realign, $fn=sides); + orient_and_anchor([2*rr,2*rr,0], UP, anchor, spin=spin, geometry="cylinder", two_d=true, chain=true) { polygon(pts); children(); } } -function circle(r=undef, d=undef, anchor=CENTER, spin=0) = +function circle(r=undef, d=undef, realign=false, circum=false, anchor=CENTER, spin=0) = let( r = get_radius(r=r, d=d, dflt=1), sides = segs(r), - pts = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]] - ) rot(spin, p=move(-normalize(anchor)*r, p=pts)); + offset = realign? 180/sides : 0, + rr = r / (circum? cos(180/sides) : 1), + pts = [for (i=[0:1:sides-1]) let(a=360-offset-i*360/sides) rr*[cos(a),sin(a)]] + ) rot(spin, p=move(-normalize(anchor)*rr, p=pts));