Added circum= and realign= args to circle()

This commit is contained in:
Revar Desmera 2019-11-03 18:44:14 -08:00
parent 914ff7fae9
commit faf12b6e26

View file

@ -55,11 +55,13 @@ function square(size=1, center=undef, anchor=FRONT+LEFT, spin=0) =
// Usage: // Usage:
// circle(r|d, [anchor]) // circle(r|d, [anchor])
// Description: // Description:
// When called as a module, creates a 2D 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 path/list of points for 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: // Arguments:
// r = The radius of the circle to create. // r = The radius of the circle to create.
// d = The diameter 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` // 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` // spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// Example(2D): By Radius // 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); // circle(d=50, anchor=FRONT, spin=45);
// Example(NORENDER): Called as Function // Example(NORENDER): Called as Function
// path = circle(d=50, anchor=FRONT, spin=45); // 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); r = get_radius(r=r, d=d, dflt=1);
sides = segs(r); sides = segs(r);
pts = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]]; rr = circum? r/cos(180/sides) : r;
orient_and_anchor([2*r,2*r,0], UP, anchor, spin=spin, geometry="cylinder", two_d=true, chain=true) { 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); polygon(pts);
children(); 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( let(
r = get_radius(r=r, d=d, dflt=1), r = get_radius(r=r, d=d, dflt=1),
sides = segs(r), sides = segs(r),
pts = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]] offset = realign? 180/sides : 0,
) rot(spin, p=move(-normalize(anchor)*r, p=pts)); 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));