Added r/d alternate args to or/od for polygon 2D shapes.

This commit is contained in:
Revar Desmera 2019-08-03 02:18:40 -07:00
parent 6ba7bde38b
commit 6c48c27baf
6 changed files with 39 additions and 35 deletions

View File

@ -83,12 +83,16 @@ function all_defined(v,recursive=false) = max([for (x=v) is_undef(x)||(recursive
// Arguments:
// r1 = Most specific radius.
// d1 = Most specific diameter.
// r2 = Second most specific radius.
// d2 = Second most specific diameter.
// r = Most general radius.
// d = Most general diameter.
// dflt = Value to return if all other values given are `undef`.
function get_radius(r1=undef, r=undef, d1=undef, d=undef, dflt=undef) = (
function get_radius(r1=undef, r2=undef, r=undef, d1=undef, d2=undef, d=undef, dflt=undef) = (
!is_undef(r1)? r1 :
!is_undef(r2)? r2 :
!is_undef(d1)? d1/2 :
!is_undef(d2)? d2/2 :
!is_undef(r)? r :
!is_undef(d)? d/2 :
dflt

View File

@ -59,8 +59,8 @@ module knurled_cylinder(
rounding=undef, rounding1=undef, rounding2=undef,
anchor=CENTER, spin=0, orient=UP
) {
r1 = get_radius(r1=r1,r=r,d1=d1,d=d,dflt=10);
r2 = get_radius(r1=r2,r=r,d1=d2,d=d,dflt=10);
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=10);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=10);
inset = r1 * sin(180/count) / tan(profile/2);
twist = 360*l*tan(helix)/(r1*2*PI);
c1 = circle(r=r1,$fn=count);
@ -156,8 +156,8 @@ module knurled_cylinder_mask(
count=30, profile=120, helix=30,
anchor=CENTER, spin=0, orient=UP
) {
r1 = get_radius(r1=r1,r=r,d1=d1,d=d,dflt=10);
r2 = get_radius(r1=r2,r=r,d1=d2,d=d,dflt=10);
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=10);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=10);
orient_and_anchor([2*r1,2*r1,l], size2=[2*r2,2*r2], anchor=anchor, spin=spin, orient=orient, geometry="cylinder", chain=true) {
difference() {
cylinder(r1=r1+overage, r2=r2+overage, h=l, center=true);

View File

@ -38,8 +38,8 @@ module angle_pie_mask(
anchor=CENTER, spin=0, orient=UP
) {
l = first_defined([l, h, 1]);
r1 = get_radius(r1, r, d1, d, 10);
r2 = get_radius(r2, r, d2, d, 10);
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=10);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=10);
orient_and_anchor([2*r1, 2*r1, l], orient, anchor, spin=spin, chain=true) {
pie_slice(ang=ang, l=l+0.1, r1=r1, r2=r2, anchor=CENTER);
children();

View File

@ -584,8 +584,8 @@ module cyl(
circum=false, realign=false, from_end=false,
anchor=CENTER, spin=0, orient=UP, center=undef
) {
r1 = get_radius(r1, r, d1, d, 1);
r2 = get_radius(r2, r, d2, d, 1);
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=1);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=1);
l = first_defined([l, h, 1]);
size1 = [r1*2,r1*2,l];
size2 = [r2*2,r2*2,l];
@ -1214,8 +1214,8 @@ module pie_slice(
center=undef, h=undef
) {
l = first_defined([l, h, 1]);
r1 = get_radius(r1, r, d1, d, 10);
r2 = get_radius(r2, r, d2, d, 10);
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=10);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=10);
maxd = max(r1,r2)+0.1;
size = [2*r1, 2*r1, l];
orient_and_anchor(size, orient, anchor, spin=spin, center=center, geometry="cylinder", chain=true) {
@ -1358,8 +1358,8 @@ module arced_slot(
$fn2 = undef
) {
r = get_radius(r=r, d=d, dflt=2);
sr1 = get_radius(sr1, sr, sd1, sd, 2);
sr2 = get_radius(sr2, sr, sd2, sd, 2);
sr1 = get_radius(r1=sr1, r=sr, d1=sd1, d=sd, dflt=2);
sr2 = get_radius(r1=sr2, r=sr, d1=sd2, d=sd, dflt=2);
fn_minor = first_defined([$fn2, $fn]);
da = ea - sa;
size = [r+sr1, r+sr1, h];

View File

@ -251,7 +251,7 @@ function arc(N, r, angle, d, cp, points, width, thickness, start, wedge=false) =
cp = is_def(cp) ? cp : [0,0],
start = is_def(start)? start : is_vector(angle) ? angle[0] : 0,
angle = is_vector(angle)? angle[1]-angle[0] : angle,
r = get_radius(r=r,d=d),
r = get_radius(r=r, d=d),
N = max(3, is_undef(N)? ceil(segs(r)*abs(angle)/360) : N),
arcpoints = [for(i=[0:N-1]) let(theta = start + i*angle/(N-1)) r*[cos(theta),sin(theta)]+cp],
extra = wedge? [cp] : []
@ -370,17 +370,17 @@ module trapezoid(h, w1, w2, anchor=CENTER, spin=0)
// regular_ngon(n=8, side=20, realign=true);
// Example(2D): Called as Function
// stroke(closed=true, regular_ngon(n=6, or=30));
function regular_ngon(n=6, or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0) =
function regular_ngon(n=6, r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0) =
let(
sc = 1/cos(180/n),
r = get_radius(r1=ir*sc, r=or, d1=id*sc, d=od, dflt=side/2/sin(180/n)),
r = get_radius(r1=ir*sc, r2=or, r=r, d1=id*sc, d2=od, d=d, dflt=side/2/sin(180/n)),
offset = 90 + (realign? (180/n) : 0),
path = [for (a=[0:360/n:360-EPSILON]) r*[cos(a+offset),sin(a+offset)]]
) rot(spin, p=move(-r*normalize(anchor), p=path));
module regular_ngon(n=6, or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0)
polygon(regular_ngon(n=n,or=or,od=od,ir=ir,id=id,side=side,realign=realign, anchor=anchor, spin=spin));
module regular_ngon(n=6, r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0)
polygon(regular_ngon(n=n,r=r,d=d,or=or,od=od,ir=ir,id=id,side=side,realign=realign, anchor=anchor, spin=spin));
// Function&Module: pentagon()
@ -412,12 +412,12 @@ module regular_ngon(n=6, or=undef, od=undef, ir=undef, id=undef, side=undef, rea
// pentagon(side=20, realign=true);
// Example(2D): Called as Function
// stroke(closed=true, pentagon(or=30));
function pentagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=5, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
function pentagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=5, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
module pentagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0)
polygon(pentagon(or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
module pentagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0)
polygon(pentagon(r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
// Function&Module: hexagon()
@ -447,12 +447,12 @@ module pentagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=fals
// hexagon(side=20, realign=true);
// Example(2D): Called as Function
// stroke(closed=true, hexagon(or=30));
function hexagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=6, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
function hexagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=6, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
module hexagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0)
polygon(hexagon(or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
module hexagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0)
polygon(hexagon(r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
// Function&Module: octagon()
@ -482,12 +482,12 @@ module hexagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false
// octagon(side=20, realign=true);
// Example(2D): Called as Function
// stroke(closed=true, octagon(or=30));
function octagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=8, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
function octagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0) =
regular_ngon(n=8, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin);
module octagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false, anchor=CENTER, spin=0)
polygon(octagon(or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
module octagon(r, d, or, od, ir, id, side, realign=false, anchor=CENTER, spin=0)
polygon(octagon(r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, realign=realign, anchor=anchor, spin=spin));
// Function&Module: glued_circles()
@ -510,7 +510,7 @@ module octagon(or=undef, od=undef, ir=undef, id=undef, side=undef, realign=false
// glued_circles(d=30, spread=30, tangent=-30);
// Example(2D): Called as Function
// stroke(closed=true, glued_circles(r=15, spread=40, tangent=45));
function glued_circles(r=undef, d=undef, spread=10, tangent=30, anchor=CENTER, spin=0) =
function glued_circles(r, d, spread=10, tangent=30, anchor=CENTER, spin=0) =
let(
r = get_radius(r=r, d=d, dflt=10),
r2 = (spread/2 / sin(tangent)) - r,
@ -536,7 +536,7 @@ function glued_circles(r=undef, d=undef, spread=10, tangent=30, anchor=CENTER, s
) rot(spin, p=move(-vmul(anchor,s), p=path));
module glued_circles(r=undef, d=undef, spread=10, tangent=30, anchor=CENTER, spin=0)
module glued_circles(r, d, spread=10, tangent=30, anchor=CENTER, spin=0)
polygon(glued_circles(r=r, d=d, spread=spread, tangent=tangent, anchor=anchor, spin=spin));
@ -631,7 +631,7 @@ function _superformula(theta,m1,m2,n1,n2=1,n3=1,a=1,b=1) =
// for(i=[1:3]) right(2.5*i)supershape(step=.5,m1=88, m2=64, n1=-i*i,n2=1,r=1);
function supershape(step=0.5,m1=4,m2=undef,n1=1,n2=undef,n3=undef,a=1,b=undef,r=undef,d=undef,anchor=CENTER, spin=0) =
let(
r = get_radius(r=r,d=d,dflt=undef),
r = get_radius(r=r, d=d, dflt=undef),
m2 = is_def(m2) ? m2 : m1,
n2 = is_def(n2) ? n2 : n1,
n3 = is_def(n3) ? n3 : n2,

View File

@ -1530,8 +1530,8 @@ module arc_of(
sa=0, ea=360,
rot=true
) {
rx = get_radius(rx, r, dx, d, 1);
ry = get_radius(ry, r, dy, d, 1);
rx = get_radius(r1=rx, r=r, d1=dx, d=d, dflt=1);
ry = get_radius(r1=ry, r=r, d1=dy, d=d, dflt=1);
sa = posmod(sa, 360);
ea = posmod(ea, 360);
n = (abs(ea-sa)<0.01)?(n+1):n;