BOSL2/shapes2d.scad
Adrian Mariano 5153cb20da doc tweak
2021-10-30 18:07:43 -04:00

1785 lines
77 KiB
OpenSCAD

//////////////////////////////////////////////////////////////////////
// LibFile: shapes2d.scad
// This file includes redefinitions of the core modules to
// work with attachment. You can also create regular polygons
// with optional rounded corners and alignment features not
// available with circle(). The file also provides teardrop2d,
// which is useful for 3d printable holes. Lastly you can use the
// masks to produce edge treatments common in furniture from the
// simple roundover or cove molding to the more elaborate ogee.
// Many of the commands have module forms that produce geometry and
// function forms that produce a path. This file defines function
// forms of the core OpenSCAD modules that produce paths.
// Includes:
// include <BOSL2/std.scad>
//////////////////////////////////////////////////////////////////////
use <builtins.scad>
// Section: 2D Primitives
// Function&Module: square()
// Topics: Shapes (2D), Path Generators (2D)
// Usage: As a Module
// square(size, [center], ...);
// Usage: With Attachments
// square(size, [center], ...) { attachables }
// Usage: As a Function
// path = square(size, [center], ...);
// See Also: rect()
// Description:
// When called as the builtin module, creates a 2D square or rectangle of the given size.
// When called as a function, returns a 2D path/list of points for a square/rectangle of the given size.
// Arguments:
// size = The size of the square to create. If given as a scalar, both X and Y will be the same size.
// center = If given and true, overrides `anchor` to be `CENTER`. If given and false, overrides `anchor` to be `FRONT+LEFT`.
// ---
// 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):
// square(40);
// Example(2D): Centered
// square([40,30], center=true);
// Example(2D): Called as Function
// path = square([40,30], anchor=FRONT, spin=30);
// stroke(path, closed=true);
// move_copies(path) color("blue") circle(d=2,$fn=8);
function square(size=1, center, anchor, spin=0) =
let(
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]),
size = is_num(size)? [size,size] : point2d(size),
path = [
[ size.x,-size.y],
[-size.x,-size.y],
[-size.x, size.y],
[ size.x, size.y]
] / 2
) reorient(anchor,spin, two_d=true, size=size, p=path);
module square(size=1, center, anchor, spin) {
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]);
size = is_num(size)? [size,size] : point2d(size);
attachable(anchor,spin, two_d=true, size=size) {
_square(size, center=true);
children();
}
}
// Function&Module: rect()
// Usage: As Module
// rect(size, [center], [rounding], [chamfer], ...);
// Usage: With Attachments
// rect(size, [center], ...) { attachables }
// Usage: As Function
// path = rect(size, [center], [rounding], [chamfer], ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: square()
// Description:
// When called as a module, creates a 2D rectangle of the given size, with optional rounding or chamfering.
// When called as a function, returns a 2D path/list of points for a square/rectangle of the given size.
// Arguments:
// size = The size of the rectangle to create. If given as a scalar, both X and Y will be the same size.
// rounding = The rounding radius for the corners. If given as a list of four numbers, gives individual radii for each corner, in the order [X+Y+,X-Y+,X-Y-,X+Y-]. Default: 0 (no rounding)
// chamfer = The chamfer size for the corners. If given as a list of four numbers, gives individual chamfers for each corner, in the order [X+Y+,X-Y+,X-Y-,X+Y-]. Default: 0 (no chamfer)
// center = If given and true, overrides `anchor` to be `CENTER`. If given and false, overrides `anchor` to be `FRONT+LEFT`.
// 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):
// rect(40);
// Example(2D): Centered
// rect([40,30], center=true);
// Example(2D): Anchored
// rect([40,30], anchor=FRONT);
// Example(2D): Spun
// rect([40,30], anchor=FRONT, spin=30);
// Example(2D): Chamferred Rect
// rect([40,30], chamfer=5, center=true);
// Example(2D): Rounded Rect
// rect([40,30], rounding=5, center=true);
// Example(2D): Mixed Chamferring and Rounding
// rect([40,30],center=true,rounding=[5,0,10,0],chamfer=[0,8,0,15],$fa=1,$fs=1);
// Example(2D): Called as Function
// path = rect([40,30], chamfer=5, anchor=FRONT, spin=30);
// stroke(path, closed=true);
// move_copies(path) color("blue") circle(d=2,$fn=8);
module rect(size=1, center, rounding=0, chamfer=0, anchor, spin=0) {
size = is_num(size)? [size,size] : point2d(size);
anchor = get_anchor(anchor, center, FRONT+LEFT, FRONT+LEFT);
if (rounding==0 && chamfer==0) {
attachable(anchor,spin, two_d=true, size=size) {
square(size, center=true);
children();
}
} else {
pts = rect(size=size, rounding=rounding, chamfer=chamfer, center=true);
attachable(anchor,spin, two_d=true, path=pts) {
polygon(pts);
children();
}
}
}
function rect(size=1, center, rounding=0, chamfer=0, anchor, spin=0) =
assert(is_num(size) || is_vector(size))
assert(is_num(chamfer) || len(chamfer)==4)
assert(is_num(rounding) || len(rounding)==4)
let(
size = is_num(size)? [size,size] : point2d(size),
anchor = point2d(get_anchor(anchor, center, FRONT+LEFT, FRONT+LEFT)),
complex = rounding!=0 || chamfer!=0
)
(rounding==0 && chamfer==0)? let(
path = [
[ size.x/2, -size.y/2],
[-size.x/2, -size.y/2],
[-size.x/2, size.y/2],
[ size.x/2, size.y/2]
]
) rot(spin, p=move(-v_mul(anchor,size/2), p=path)) :
let(
chamfer = is_list(chamfer)? chamfer : [for (i=[0:3]) chamfer],
rounding = is_list(rounding)? rounding : [for (i=[0:3]) rounding],
quadorder = [3,2,1,0],
quadpos = [[1,1],[-1,1],[-1,-1],[1,-1]],
insets = [for (i=[0:3]) chamfer[i]>0? chamfer[i] : rounding[i]>0? rounding[i] : 0],
insets_x = max(insets[0]+insets[1],insets[2]+insets[3]),
insets_y = max(insets[0]+insets[3],insets[1]+insets[2])
)
assert(insets_x <= size.x, "Requested roundings and/or chamfers exceed the rect width.")
assert(insets_y <= size.y, "Requested roundings and/or chamfers exceed the rect height.")
let(
path = [
for(i = [0:3])
let(
quad = quadorder[i],
inset = insets[quad],
cverts = quant(segs(inset),4)/4,
cp = v_mul(size/2-[inset,inset], quadpos[quad]),
step = 90/cverts,
angs =
chamfer[quad] > 0? [0,-90]-90*[i,i] :
rounding[quad] > 0? [for (j=[0:1:cverts]) 360-j*step-i*90] :
[0]
)
each [for (a = angs) cp + inset*[cos(a),sin(a)]]
]
) complex?
reorient(anchor,spin, two_d=true, path=path, p=path) :
reorient(anchor,spin, two_d=true, size=size, p=path);
// Function&Module: circle()
// Topics: Shapes (2D), Path Generators (2D)
// Usage: As a Module
// circle(r|d=, ...);
// Usage: With Attachments
// circle(r|d=, ...) { attachables }
// Usage: As a Function
// path = circle(r|d=, ...);
// See Also: oval()
// Description:
// When called as the builtin 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.
// ---
// 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
// circle(r=25);
// Example(2D): By Diameter
// circle(d=50);
// Example(NORENDER): Called as Function
// path = circle(d=50, anchor=FRONT, spin=45);
function circle(r, d, anchor=CENTER, spin=0) =
let(
r = get_radius(r=r, d=d, dflt=1),
sides = segs(r),
path = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]]
) reorient(anchor,spin, two_d=true, r=r, p=path);
module circle(r, d, anchor=CENTER, spin=0) {
r = get_radius(r=r, d=d, dflt=1);
attachable(anchor,spin, two_d=true, r=r) {
_circle(r=r);
children();
}
}
// Function&Module: oval()
// Usage: As a Module
// oval(r|d=, [realign=], [circum=], ...);
// Usage: With Attachments
// oval(r|d=, [realign=], [circum=], ...) { attachables }
// Usage: As a Function
// path = oval(r|d=, [realign=], [circum=], ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle()
// Description:
// 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 = Radius of the circle/oval to create. Can be a scalar, or a list of sizes per axis.
// ---
// d = Diameter of the circle/oval to create. Can be a scalar, or a list of sizes per axis.
// realign = If true, rotates the polygon that approximates the circle/oval 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
// oval(r=25);
// Example(2D): By Diameter
// oval(d=50);
// Example(2D): Anchoring
// oval(d=50, anchor=FRONT);
// Example(2D): Spin
// oval(d=50, anchor=FRONT, spin=45);
// Example(NORENDER): Called as Function
// path = oval(d=50, anchor=FRONT, spin=45);
module oval(r, d, realign=false, circum=false, anchor=CENTER, spin=0) {
r = get_radius(r=r, d=d, dflt=1);
sides = segs(max(r));
sc = circum? (1 / cos(180/sides)) : 1;
rx = default(r[0],r) * sc;
ry = default(r[1],r) * sc;
attachable(anchor,spin, two_d=true, r=[rx,ry]) {
if (rx < ry) {
xscale(rx/ry) {
zrot(realign? 180/sides : 0) {
circle(r=ry, $fn=sides);
}
}
} else {
yscale(ry/rx) {
zrot(realign? 180/sides : 0) {
circle(r=rx, $fn=sides);
}
}
}
children();
}
}
function oval(r, d, realign=false, circum=false, anchor=CENTER, spin=0) =
let(
r = get_radius(r=r, d=d, dflt=1),
sides = segs(max(r)),
offset = realign? 180/sides : 0,
sc = circum? (1 / cos(180/sides)) : 1,
rx = default(r[0],r) * sc,
ry = default(r[1],r) * sc,
pts = [for (i=[0:1:sides-1]) let(a=360-offset-i*360/sides) [rx*cos(a), ry*sin(a)]]
) reorient(anchor,spin, two_d=true, r=[rx,ry], p=pts);
// Section: Polygons
// Function&Module: regular_ngon()
// Usage:
// regular_ngon(n, r/d=/or=/od=, [realign=]);
// regular_ngon(n, ir=/id=, [realign=]);
// regular_ngon(n, side=, [realign=]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), pentagon(), hexagon(), octagon(), oval(), star()
// Description:
// When called as a function, returns a 2D path for a regular N-sided polygon.
// When called as a module, creates a 2D regular N-sided polygon.
// Arguments:
// n = The number of sides.
// r/or = Outside radius, at points.
// ---
// d/od = Outside diameter, at points.
// ir = Inside radius, at center of sides.
// id = Inside diameter, at center of sides.
// side = Length of each side.
// rounding = Radius of rounding for the tips of the polygon. Default: 0 (no rounding)
// realign = If false, a tip is aligned with the Y+ axis. If true, the midpoint of a side is aligned with the Y+ axis. Default: false
// align_tip = If given as a 2D vector, rotates the whole shape so that the first vertex points in that direction. This occurs before spin.
// align_side = If given as a 2D vector, rotates the whole shape so that the normal of side0 points in that direction. This occurs before spin.
// 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`
// Extra Anchors:
// "tip0", "tip1", etc. = Each tip has an anchor, pointing outwards.
// "side0", "side1", etc. = The center of each side has an anchor, pointing outwards.
// Example(2D): by Outer Size
// regular_ngon(n=5, or=30);
// regular_ngon(n=5, od=60);
// Example(2D): by Inner Size
// regular_ngon(n=5, ir=30);
// regular_ngon(n=5, id=60);
// Example(2D): by Side Length
// regular_ngon(n=8, side=20);
// Example(2D): Realigned
// regular_ngon(n=8, side=20, realign=true);
// Example(2D): Alignment by Tip
// regular_ngon(n=5, r=30, align_tip=BACK+RIGHT)
// attach("tip0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Alignment by Side
// regular_ngon(n=5, r=30, align_side=BACK+RIGHT)
// attach("side0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Rounded
// regular_ngon(n=5, od=100, rounding=20, $fn=20);
// Example(2D): Called as Function
// stroke(closed=true, regular_ngon(n=6, or=30));
function regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0, _mat, _anchs) =
assert(is_undef(align_tip) || is_vector(align_tip))
assert(is_undef(align_side) || is_vector(align_side))
assert(is_undef(align_tip) || is_undef(align_side), "Can only specify one of align_tip and align-side")
let(
sc = 1/cos(180/n),
ir = is_finite(ir)? ir*sc : undef,
id = is_finite(id)? id*sc : undef,
side = is_finite(side)? side/2/sin(180/n) : undef,
r = get_radius(r1=ir, r2=or, r=r, d1=id, d2=od, d=d, dflt=side)
)
assert(!is_undef(r), "regular_ngon(): need to specify one of r, d, or, od, ir, id, side.")
let(
inset = opp_ang_to_hyp(rounding, (180-360/n)/2),
mat = !is_undef(_mat) ? _mat :
( realign? rot(-180/n, planar=true) : affine2d_identity() ) * (
!is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip), planar=true) :
!is_undef(align_side)? rot(from=RIGHT, to=point2d(align_side), planar=true) * rot(180/n, planar=true) :
affine2d_identity()
),
path4 = rounding==0? oval(r=r, $fn=n) : (
let(
steps = floor(segs(r)/n),
step = 360/n/steps,
path2 = [
for (i = [0:1:n-1]) let(
a = 360 - i*360/n,
p = polar_to_xy(r-inset, a)
)
each arc(N=steps, cp=p, r=rounding, start=a+180/n, angle=-360/n)
],
maxx_idx = max_index(column(path2,0)),
path3 = polygon_shift(path2,maxx_idx)
) path3
),
path = apply(mat, path4),
anchors = !is_undef(_anchs) ? _anchs :
!is_string(anchor)? [] : [
for (i = [0:1:n-1]) let(
a1 = 360 - i*360/n,
a2 = a1 - 360/n,
p1 = apply(mat, polar_to_xy(r,a1)),
p2 = apply(mat, polar_to_xy(r,a2)),
tipp = apply(mat, polar_to_xy(r-inset+rounding,a1)),
pos = (p1+p2)/2
) each [
named_anchor(str("tip",i), tipp, unit(tipp,BACK), 0),
named_anchor(str("side",i), pos, unit(pos,BACK), 0),
]
]
) reorient(anchor,spin, two_d=true, path=path, extent=false, p=path, anchors=anchors);
module regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0) {
sc = 1/cos(180/n);
ir = is_finite(ir)? ir*sc : undef;
id = is_finite(id)? id*sc : undef;
side = is_finite(side)? side/2/sin(180/n) : undef;
r = get_radius(r1=ir, r2=or, r=r, d1=id, d2=od, d=d, dflt=side);
assert(!is_undef(r), "regular_ngon(): need to specify one of r, d, or, od, ir, id, side.");
mat = ( realign? rot(-180/n, planar=true) : affine2d_identity() ) * (
!is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip), planar=true) :
!is_undef(align_side)? rot(from=RIGHT, to=point2d(align_side), planar=true) * rot(180/n, planar=true) :
affine2d_identity()
);
inset = opp_ang_to_hyp(rounding, (180-360/n)/2);
anchors = [
for (i = [0:1:n-1]) let(
a1 = 360 - i*360/n,
a2 = a1 - 360/n,
p1 = apply(mat, polar_to_xy(r,a1)),
p2 = apply(mat, polar_to_xy(r,a2)),
tipp = apply(mat, polar_to_xy(r-inset+rounding,a1)),
pos = (p1+p2)/2
) each [
named_anchor(str("tip",i), tipp, unit(tipp,BACK), 0),
named_anchor(str("side",i), pos, unit(pos,BACK), 0),
]
];
path = regular_ngon(n=n, r=r, rounding=rounding, _mat=mat, _anchs=anchors);
attachable(anchor,spin, two_d=true, path=path, extent=false, anchors=anchors) {
polygon(path);
children();
}
}
// Function&Module: pentagon()
// Usage:
// pentagon(or|od=, [realign=]);
// pentagon(ir=|id=, [realign=]);
// pentagon(side=, [realign=]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), regular_ngon(), hexagon(), octagon(), oval(), star()
// Description:
// When called as a function, returns a 2D path for a regular pentagon.
// When called as a module, creates a 2D regular pentagon.
// Arguments:
// r/or = Outside radius, at points.
// ---
// d/od = Outside diameter, at points.
// ir = Inside radius, at center of sides.
// id = Inside diameter, at center of sides.
// side = Length of each side.
// rounding = Radius of rounding for the tips of the polygon. Default: 0 (no rounding)
// realign = If false, a tip is aligned with the Y+ axis. If true, the midpoint of a side is aligned with the Y+ axis. Default: false
// align_tip = If given as a 2D vector, rotates the whole shape so that the first vertex points in that direction. This occurs before spin.
// align_side = If given as a 2D vector, rotates the whole shape so that the normal of side0 points in that direction. This occurs before spin.
// 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`
// Extra Anchors:
// "tip0" ... "tip4" = Each tip has an anchor, pointing outwards.
// "side0" ... "side4" = The center of each side has an anchor, pointing outwards.
// Example(2D): by Outer Size
// pentagon(or=30);
// pentagon(od=60);
// Example(2D): by Inner Size
// pentagon(ir=30);
// pentagon(id=60);
// Example(2D): by Side Length
// pentagon(side=20);
// Example(2D): Realigned
// pentagon(side=20, realign=true);
// Example(2D): Alignment by Tip
// pentagon(r=30, align_tip=BACK+RIGHT)
// attach("tip0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Alignment by Side
// pentagon(r=30, align_side=BACK+RIGHT)
// attach("side0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Rounded
// pentagon(od=100, rounding=20, $fn=20);
// Example(2D): Called as Function
// stroke(closed=true, pentagon(or=30));
function pentagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0) =
regular_ngon(n=5, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin);
module pentagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0)
regular_ngon(n=5, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin) children();
// Function&Module: hexagon()
// Usage: As Module
// hexagon(r/or, [realign=], <align_tip=|align_side=>, [rounding=], ...);
// hexagon(d=/od=, ...);
// hexagon(ir=/id=, ...);
// hexagon(side=, ...);
// Usage: With Attachments
// hexagon(r/or, ...) { attachments }
// Usage: As Function
// path = hexagon(r/or, ...);
// path = hexagon(d=/od=, ...);
// path = hexagon(ir=/id=, ...);
// path = hexagon(side=, ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), regular_ngon(), pentagon(), octagon(), oval(), star()
// Description:
// When called as a function, returns a 2D path for a regular hexagon.
// When called as a module, creates a 2D regular hexagon.
// Arguments:
// r/or = Outside radius, at points.
// ---
// d/od = Outside diameter, at points.
// ir = Inside radius, at center of sides.
// id = Inside diameter, at center of sides.
// side = Length of each side.
// rounding = Radius of rounding for the tips of the polygon. Default: 0 (no rounding)
// realign = If false, a tip is aligned with the Y+ axis. If true, the midpoint of a side is aligned with the Y+ axis. Default: false
// align_tip = If given as a 2D vector, rotates the whole shape so that the first vertex points in that direction. This occurs before spin.
// align_side = If given as a 2D vector, rotates the whole shape so that the normal of side0 points in that direction. This occurs before spin.
// 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`
// Extra Anchors:
// "tip0" ... "tip5" = Each tip has an anchor, pointing outwards.
// "side0" ... "side5" = The center of each side has an anchor, pointing outwards.
// Example(2D): by Outer Size
// hexagon(or=30);
// hexagon(od=60);
// Example(2D): by Inner Size
// hexagon(ir=30);
// hexagon(id=60);
// Example(2D): by Side Length
// hexagon(side=20);
// Example(2D): Realigned
// hexagon(side=20, realign=true);
// Example(2D): Alignment by Tip
// hexagon(r=30, align_tip=BACK+RIGHT)
// attach("tip0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Alignment by Side
// hexagon(r=30, align_side=BACK+RIGHT)
// attach("side0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Rounded
// hexagon(od=100, rounding=20, $fn=20);
// Example(2D): Called as Function
// stroke(closed=true, hexagon(or=30));
function hexagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0) =
regular_ngon(n=6, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin);
module hexagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0)
regular_ngon(n=6, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin) children();
// Function&Module: octagon()
// Usage: As Module
// octagon(r/or, [realign=], <align_tip=|align_side=>, [rounding=], ...);
// octagon(d=/od=, ...);
// octagon(ir=/id=, ...);
// octagon(side=, ...);
// Usage: With Attachments
// octagon(r/or, ...) { attachments }
// Usage: As Function
// path = octagon(r/or, ...);
// path = octagon(d=/od=, ...);
// path = octagon(ir=/id=, ...);
// path = octagon(side=, ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), regular_ngon(), pentagon(), hexagon(), oval(), star()
// Description:
// When called as a function, returns a 2D path for a regular octagon.
// When called as a module, creates a 2D regular octagon.
// Arguments:
// r/or = Outside radius, at points.
// d/od = Outside diameter, at points.
// ir = Inside radius, at center of sides.
// id = Inside diameter, at center of sides.
// side = Length of each side.
// rounding = Radius of rounding for the tips of the polygon. Default: 0 (no rounding)
// realign = If false, a tip is aligned with the Y+ axis. If true, the midpoint of a side is aligned with the Y+ axis. Default: false
// align_tip = If given as a 2D vector, rotates the whole shape so that the first vertex points in that direction. This occurs before spin.
// align_side = If given as a 2D vector, rotates the whole shape so that the normal of side0 points in that direction. This occurs before spin.
// 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`
// Extra Anchors:
// "tip0" ... "tip7" = Each tip has an anchor, pointing outwards.
// "side0" ... "side7" = The center of each side has an anchor, pointing outwards.
// Example(2D): by Outer Size
// octagon(or=30);
// octagon(od=60);
// Example(2D): by Inner Size
// octagon(ir=30);
// octagon(id=60);
// Example(2D): by Side Length
// octagon(side=20);
// Example(2D): Realigned
// octagon(side=20, realign=true);
// Example(2D): Alignment by Tip
// octagon(r=30, align_tip=BACK+RIGHT)
// attach("tip0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Alignment by Side
// octagon(r=30, align_side=BACK+RIGHT)
// attach("side0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Rounded
// octagon(od=100, rounding=20, $fn=20);
// Example(2D): Called as Function
// stroke(closed=true, octagon(or=30));
function octagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0) =
regular_ngon(n=8, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin);
module octagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, align_side, anchor=CENTER, spin=0)
regular_ngon(n=8, r=r, d=d, or=or, od=od, ir=ir, id=id, side=side, rounding=rounding, realign=realign, align_tip=align_tip, align_side=align_side, anchor=anchor, spin=spin) children();
// Function&Module: right_triangle()
// Usage: As Module
// right_triangle(size, [center], ...);
// Usage: With Attachments
// right_triangle(size, [center], ...) { attachments }
// Usage: As Function
// path = right_triangle(size, [center], ...);
// Description:
// Creates a right triangle with the Hypotenuse in the X+Y+ quadrant.
// Arguments:
// size = The width and length of the right triangle, given as a scalar or an XY vector.
// center = If true, forces `anchor=CENTER`. If false, forces `anchor=[-1,-1]`. Default: undef (use `anchor=`)
// ---
// 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:
// right_triangle([40,30]);
// Example: With `center=true`
// right_triangle([40,30], center=true);
// Example: Anchors
// right_triangle([40,30])
// show_anchors();
function right_triangle(size=[1,1], center, anchor, spin=0) =
let(
size = is_num(size)? [size,size] : size,
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1])
)
assert(is_vector(size,2))
let(
path = [ [size.x/2,-size.y/2], [-size.x/2,-size.y/2], [-size.x/2,size.y/2] ]
) reorient(anchor,spin, two_d=true, size=[size.x,size.y], size2=0, shift=-size.x/2, p=path);
module right_triangle(size=[1,1], center, anchor, spin=0) {
size = is_num(size)? [size,size] : size;
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]);
assert(is_vector(size,2));
path = right_triangle(size, center=true);
attachable(anchor,spin, two_d=true, size=[size.x,size.y], size2=0, shift=-size.x/2) {
polygon(path);
children();
}
}
// Function&Module: trapezoid()
// Usage: As Module
// trapezoid(h, w1, w2, [shift=], [rounding=], [chamfer=], ...);
// trapezoid(h, w1, angle=, ...);
// trapezoid(h, w2, angle=, ...);
// trapezoid(w1, w2, angle=, ...);
// Usage: With Attachments
// trapezoid(h, w1, w2, ...) { attachments }
// Usage: As Function
// path = trapezoid(h, w1, w2, ...);
// path = trapezoid(h, w1, angle=, ...);
// path = trapezoid(h, w2=, angle=, ...);
// path = trapezoid(w1=, w2=, angle=, ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: rect(), square()
// Description:
// When called as a function, returns a 2D path for a trapezoid with parallel front and back sides.
// When called as a module, creates a 2D trapezoid with parallel front and back sides.
// Arguments:
// h = The Y axis height of the trapezoid.
// w1 = The X axis width of the front end of the trapezoid.
// w2 = The X axis width of the back end of the trapezoid.
// ---
// angle = If given in place of `h`, `w1`, or `w2`, then the missing value is calculated such that the right side has that angle away from the Y axis.
// shift = Scalar value to shift the back of the trapezoid along the X axis by. Default: 0
// rounding = The rounding radius for the corners. If given as a list of four numbers, gives individual radii for each corner, in the order [X+Y+,X-Y+,X-Y-,X+Y-]. Default: 0 (no rounding)
// chamfer = The Length of the chamfer faces at the corners. If given as a list of four numbers, gives individual chamfers for each corner, in the order [X+Y+,X-Y+,X-Y-,X+Y-]. Default: 0 (no chamfer)
// 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`
// Examples(2D):
// trapezoid(h=30, w1=40, w2=20);
// trapezoid(h=25, w1=20, w2=35);
// trapezoid(h=20, w1=40, w2=0);
// trapezoid(h=20, w1=30, angle=30);
// trapezoid(h=20, w1=20, angle=-30);
// trapezoid(h=20, w2=10, angle=30);
// trapezoid(h=20, w2=30, angle=-30);
// trapezoid(w1=30, w2=10, angle=30);
// Example(2D): Chamferred Trapezoid
// trapezoid(h=30, w1=60, w2=40, chamfer=5);
// Example(2D): Rounded Trapezoid
// trapezoid(h=30, w1=60, w2=40, rounding=5);
// Example(2D): Mixed Chamfering and Rounding
// trapezoid(h=30, w1=60, w2=40, rounding=[5,0,10,0],chamfer=[0,8,0,15],$fa=1,$fs=1);
// Example(2D): Called as Function
// stroke(closed=true, trapezoid(h=30, w1=40, w2=20));
function trapezoid(h, w1, w2, angle, shift=0, chamfer=0, rounding=0, anchor=CENTER, spin=0) =
assert(is_undef(h) || is_finite(h))
assert(is_undef(w1) || is_finite(w1))
assert(is_undef(w2) || is_finite(w2))
assert(is_undef(angle) || is_finite(angle))
assert(num_defined([h, w1, w2, angle]) == 3, "Must give exactly 3 of the arguments h, w1, w2, and angle.")
assert(is_finite(shift))
assert(is_finite(chamfer) || is_vector(chamfer,4))
assert(is_finite(rounding) || is_vector(rounding,4))
let(
simple = chamfer==0 && rounding==0,
h = !is_undef(h)? h : opp_ang_to_adj(abs(w2-w1)/2, abs(angle)),
w1 = !is_undef(w1)? w1 : w2 + 2*(adj_ang_to_opp(h, angle) + shift),
w2 = !is_undef(w2)? w2 : w1 - 2*(adj_ang_to_opp(h, angle) + shift)
)
assert(w1>=0 && w2>=0 && h>0, "Degenerate trapezoid geometry.")
assert(w1+w2>0, "Degenerate trapezoid geometry.")
let(
base_path = [
[w2/2+shift,h/2],
[-w2/2+shift,h/2],
[-w1/2,-h/2],
[w1/2,-h/2],
],
cpath = simple? base_path :
path_chamfer_and_rounding(
base_path, closed=true,
chamfer=chamfer,
rounding=rounding
),
path = reverse(cpath)
) simple
? reorient(anchor,spin, two_d=true, size=[w1,h], size2=w2, shift=shift, p=path)
: reorient(anchor,spin, two_d=true, path=path, p=path);
module trapezoid(h, w1, w2, angle, shift=0, chamfer=0, rounding=0, anchor=CENTER, spin=0) {
path = trapezoid(h=h, w1=w1, w2=w2, angle=angle, shift=shift, chamfer=chamfer, rounding=rounding);
union() {
simple = chamfer==0 && rounding==0;
h = !is_undef(h)? h : opp_ang_to_adj(abs(w2-w1)/2, abs(angle));
w1 = !is_undef(w1)? w1 : w2 + 2*(adj_ang_to_opp(h, angle) + shift);
w2 = !is_undef(w2)? w2 : w1 - 2*(adj_ang_to_opp(h, angle) + shift);
if (simple) {
attachable(anchor,spin, two_d=true, size=[w1,h], size2=w2, shift=shift) {
polygon(path);
children();
}
} else {
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
}
}
// Function&Module: star()
// Usage: As Module
// star(n, r/or, ir, [realign=], [align_tip=], [align_pit=], ...);
// star(n, r/or, step=, ...);
// Usage: With Attachments
// star(n, r/or, ir, ...) { attachments }
// Usage: As Function
// path = star(n, r/or, ir, [realign=], [align_tip=], [align_pit=], ...);
// path = star(n, r/or, step=, ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), oval()
// Description:
// When called as a function, returns the path needed to create a star polygon with N points.
// When called as a module, creates a star polygon with N points.
// Arguments:
// n = The number of stellate tips on the star.
// r/or = The radius to the tips of the star.
// ir = The radius to the inner corners of the star.
// ---
// d/od = The diameter to the tips of the star.
// id = The diameter to the inner corners of the star.
// step = Calculates the radius of the inner star corners by virtually drawing a straight line `step` tips around the star. 2 <= step < n/2
// realign = If false, a tip is aligned with the Y+ axis. If true, an inner corner is aligned with the Y+ axis. Default: false
// align_tip = If given as a 2D vector, rotates the whole shape so that the first star tip points in that direction. This occurs before spin.
// align_pit = If given as a 2D vector, rotates the whole shape so that the first inner corner is pointed towards that direction. This occurs before spin.
// 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`
// Extra Anchors:
// "tip0" ... "tip4" = Each tip has an anchor, pointing outwards.
// "pit0" ... "pit4" = The inside corner between each tip has an anchor, pointing outwards.
// "midpt0" ... "midpt4" = The center-point between each pair of tips has an anchor, pointing outwards.
// Examples(2D):
// star(n=5, r=50, ir=25);
// star(n=5, r=50, step=2);
// star(n=7, r=50, step=2);
// star(n=7, r=50, step=3);
// Example(2D): Realigned
// star(n=7, r=50, step=3, realign=true);
// Example(2D): Alignment by Tip
// star(n=5, ir=15, or=30, align_tip=BACK+RIGHT)
// attach("tip0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Alignment by Pit
// star(n=5, ir=15, or=30, align_pit=BACK+RIGHT)
// attach("pit0", FWD) color("blue")
// stroke([[0,0],[0,7]], endcap2="arrow2");
// Example(2D): Called as Function
// stroke(closed=true, star(n=5, r=50, ir=25));
function star(n, r, ir, d, or, od, id, step, realign=false, align_tip, align_pit, anchor=CENTER, spin=0, _mat, _anchs) =
assert(is_undef(align_tip) || is_vector(align_tip))
assert(is_undef(align_pit) || is_vector(align_pit))
assert(is_undef(align_tip) || is_undef(align_pit), "Can only specify one of align_tip and align_pit")
assert(is_def(n), "Must specify number of points, n")
let(
r = get_radius(r1=or, d1=od, r=r, d=d),
count = num_defined([ir,id,step]),
stepOK = is_undef(step) || (step>1 && step<n/2)
)
assert(count==1, "Must specify exactly one of ir, id, step")
assert(stepOK, n==4 ? "Parameter 'step' not allowed for 4 point stars"
: n==5 || n==6 ? str("Parameter 'step' must be 2 for ",n," point stars")
: str("Parameter 'step' must be between 2 and ",floor(n/2-1/2)," for ",n," point stars"))
let(
mat = !is_undef(_mat) ? _mat :
( realign? rot(-180/n, planar=true) : affine2d_identity() ) * (
!is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip), planar=true) :
!is_undef(align_pit)? rot(from=RIGHT, to=point2d(align_pit), planar=true) * rot(180/n, planar=true) :
affine2d_identity()
),
stepr = is_undef(step)? r : r*cos(180*step/n)/cos(180*(step-1)/n),
ir = get_radius(r=ir, d=id, dflt=stepr),
offset = realign? 180/n : 0,
path1 = [for(i=[2*n:-1:1]) let(theta=180*i/n, radius=(i%2)?ir:r) radius*[cos(theta), sin(theta)]],
path = apply(mat, path1),
anchors = !is_undef(_anchs) ? _anchs :
!is_string(anchor)? [] : [
for (i = [0:1:n-1]) let(
a1 = 360 - i*360/n,
a2 = a1 - 180/n,
a3 = a1 - 360/n,
p1 = apply(mat, polar_to_xy(r,a1)),
p2 = apply(mat, polar_to_xy(ir,a2)),
p3 = apply(mat, polar_to_xy(r,a3)),
pos = (p1+p3)/2
) each [
named_anchor(str("tip",i), p1, unit(p1,BACK), 0),
named_anchor(str("pit",i), p2, unit(p2,BACK), 0),
named_anchor(str("midpt",i), pos, unit(pos,BACK), 0),
]
]
) reorient(anchor,spin, two_d=true, path=path, p=path, anchors=anchors);
module star(n, r, ir, d, or, od, id, step, realign=false, align_tip, align_pit, anchor=CENTER, spin=0) {
assert(is_undef(align_tip) || is_vector(align_tip));
assert(is_undef(align_pit) || is_vector(align_pit));
assert(is_undef(align_tip) || is_undef(align_pit), "Can only specify one of align_tip and align_pit");
r = get_radius(r1=or, d1=od, r=r, d=d, dflt=undef);
stepr = is_undef(step)? r : r*cos(180*step/n)/cos(180*(step-1)/n);
ir = get_radius(r=ir, d=id, dflt=stepr);
mat = ( realign? rot(-180/n, planar=true) : affine2d_identity() ) * (
!is_undef(align_tip)? rot(from=RIGHT, to=point2d(align_tip), planar=true) :
!is_undef(align_pit)? rot(from=RIGHT, to=point2d(align_pit), planar=true) * rot(180/n, planar=true) :
affine2d_identity()
);
anchors = [
for (i = [0:1:n-1]) let(
a1 = 360 - i*360/n - (realign? 180/n : 0),
a2 = a1 - 180/n,
a3 = a1 - 360/n,
p1 = apply(mat, polar_to_xy(r,a1)),
p2 = apply(mat, polar_to_xy(ir,a2)),
p3 = apply(mat, polar_to_xy(r,a3)),
pos = (p1+p3)/2
) each [
named_anchor(str("tip",i), p1, unit(p1,BACK), 0),
named_anchor(str("pit",i), p2, unit(p2,BACK), 0),
named_anchor(str("midpt",i), pos, unit(pos,BACK), 0),
]
];
path = star(n=n, r=r, ir=ir, realign=realign, _mat=mat, _anchs=anchors);
attachable(anchor,spin, two_d=true, path=path, anchors=anchors) {
polygon(path);
children();
}
}
/// Internal Function: _path_add_jitter()
/// Topics: Paths
/// See Also: jittered_poly(), subdivide_long_segments()
/// Usage:
/// jpath = _path_add_jitter(path, [dist], [closed=]);
/// Description:
/// Adds tiny jitter offsets to collinear points in the given path so that they
/// are no longer collinear. This is useful for preserving subdivision on long
/// straight segments, when making geometry with `polygon()`, for use with
/// `linear_exrtrude()` with a `twist()`.
/// Arguments:
/// path = The path to add jitter to.
/// dist = The amount to jitter points by. Default: 1/512 (0.00195)
/// ---
/// closed = If true, treat path like a closed polygon. Default: true
/// Example(3D):
/// d = 100; h = 75; quadsize = 5;
/// path = pentagon(d=d);
/// spath = subdivide_long_segments(path, quadsize, closed=true);
/// jpath = _path_add_jitter(spath, closed=true);
/// linear_extrude(height=h, twist=72, slices=h/quadsize)
/// polygon(jpath);
function _path_add_jitter(path, dist=1/512, closed=true) =
assert(is_path(path))
assert(is_finite(dist))
assert(is_bool(closed))
[
path[0],
for (i=idx(path,s=1,e=closed?-1:-2)) let(
n = line_normal([path[i-1],path[i]])
) path[i] + n * (is_collinear(select(path,i-1,i+1))? (dist * ((i%2)*2-1)) : 0),
if (!closed) last(path)
];
// Module: jittered_poly()
// Topics: Extrusions
// See Also: subdivide_long_segments()
// Usage:
// jittered_poly(path, [dist]);
// Description:
// Creates a 2D polygon shape from the given path in such a way that any extra
// collinear points are not stripped out in the way that `polygon()` normally does.
// This is useful for refining the mesh of a `linear_extrude()` with twist.
// Arguments:
// path = The path to add jitter to.
// dist = The amount to jitter points by. Default: 1/512 (0.00195)
// Example:
// d = 100; h = 75; quadsize = 5;
// path = pentagon(d=d);
// spath = subdivide_long_segments(path, quadsize, closed=true);
// linear_extrude(height=h, twist=72, slices=h/quadsize)
// jittered_poly(spath);
module jittered_poly(path, dist=1/512) {
polygon(_path_add_jitter(path, dist, closed=true));
}
// Section: Curved 2D Shapes
// Function&Module: teardrop2d()
//
// Description:
// Makes a 2D teardrop shape. Useful for extruding into 3D printable holes.
//
// Usage: As Module
// teardrop2d(r/d=, [ang], [cap_h]);
// Usage: With Attachments
// teardrop2d(r/d=, [ang], [cap_h], ...) { attachments }
// Usage: As Function
// path = teardrop2d(r/d=, [ang], [cap_h]);
//
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
//
// See Also: teardrop(), onion()
//
// Arguments:
// r = radius of circular part of teardrop. (Default: 1)
// ang = angle of hat walls from the Y axis. (Default: 45 degrees)
// cap_h = if given, height above center where the shape will be truncated.
// ---
// d = diameter of spherical portion of bottom. (Use instead of r)
// 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): Typical Shape
// teardrop2d(r=30, ang=30);
// Example(2D): Crop Cap
// teardrop2d(r=30, ang=30, cap_h=40);
// Example(2D): Close Crop
// teardrop2d(r=30, ang=30, cap_h=20);
module teardrop2d(r, ang=45, cap_h, d, anchor=CENTER, spin=0)
{
path = teardrop2d(r=r, d=d, ang=ang, cap_h=cap_h);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
function teardrop2d(r, ang=45, cap_h, d, anchor=CENTER, spin=0) =
let(
r = get_radius(r=r, d=d, dflt=1),
tanpt = polar_to_xy(r, ang),
tip_y = adj_ang_to_hyp(r, 90-ang),
cap_h = min(default(cap_h,tip_y), tip_y),
cap_w = tanpt.y >= cap_h
? hyp_opp_to_adj(r, cap_h)
: adj_ang_to_opp(tip_y-cap_h, ang),
ang2 = min(ang,atan2(cap_h,cap_w)),
sa = 180 - ang2,
ea = 360 + ang2,
steps = ceil(segs(r)*(ea-sa)/360),
path = deduplicate(
[
[ cap_w,cap_h],
for (a=lerpn(ea,sa,steps+1)) r*[cos(a),sin(a)],
[-cap_w,cap_h]
], closed=true
),
maxx_idx = max_index(column(path,0)),
path2 = polygon_shift(path,maxx_idx)
) reorient(anchor,spin, two_d=true, path=path2, p=path2);
// Function&Module: glued_circles()
// Usage: As Module
// glued_circles(r/d=, [spread=], [tangent=], ...);
// Usage: With Attachments
// glued_circles(r/d=, [spread=], [tangent=], ...) { attachments }
// Usage: As Function
// path = glued_circles(r/d=, [spread=], [tangent=], ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), oval()
// Description:
// When called as a function, returns a 2D path forming a shape of two circles joined by curved waist.
// When called as a module, creates a 2D shape of two circles joined by curved waist.
// Arguments:
// r = The radius of the end circles.
// spread = The distance between the centers of the end circles. Default: 10
// tangent = The angle in degrees of the tangent point for the joining arcs, measured away from the Y axis. Default: 30
// ---
// d = The diameter of the end circles.
// 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`
// Examples(2D):
// glued_circles(r=15, spread=40, tangent=45);
// glued_circles(d=30, spread=30, tangent=30);
// glued_circles(d=30, spread=30, tangent=15);
// 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, spread=10, tangent=30, d, anchor=CENTER, spin=0) =
let(
r = get_radius(r=r, d=d, dflt=10),
r2 = (spread/2 / sin(tangent)) - r,
cp1 = [spread/2, 0],
cp2 = [0, (r+r2)*cos(tangent)],
sa1 = 90-tangent,
ea1 = 270+tangent,
lobearc = ea1-sa1,
lobesegs = ceil(segs(r)*lobearc/360),
lobestep = lobearc / lobesegs,
sa2 = 270-tangent,
ea2 = 270+tangent,
subarc = ea2-sa2,
arcsegs = ceil(segs(r2)*abs(subarc)/360),
arcstep = subarc / arcsegs,
path = concat(
[for (i=[0:1:lobesegs]) let(a=sa1+i*lobestep) r * [cos(a),sin(a)] - cp1],
tangent==0? [] : [for (i=[0:1:arcsegs]) let(a=ea2-i*arcstep+180) r2 * [cos(a),sin(a)] - cp2],
[for (i=[0:1:lobesegs]) let(a=sa1+i*lobestep+180) r * [cos(a),sin(a)] + cp1],
tangent==0? [] : [for (i=[0:1:arcsegs]) let(a=ea2-i*arcstep) r2 * [cos(a),sin(a)] + cp2]
),
maxx_idx = max_index(column(path,0)),
path2 = reverse_polygon(polygon_shift(path,maxx_idx))
) reorient(anchor,spin, two_d=true, path=path2, extent=true, p=path2);
module glued_circles(r, spread=10, tangent=30, d, anchor=CENTER, spin=0) {
path = glued_circles(r=r, d=d, spread=spread, tangent=tangent);
attachable(anchor,spin, two_d=true, path=path, extent=true) {
polygon(path);
children();
}
}
function _superformula(theta,m1,m2,n1,n2=1,n3=1,a=1,b=1) =
pow(pow(abs(cos(m1*theta/4)/a),n2)+pow(abs(sin(m2*theta/4)/b),n3),-1/n1);
// Function&Module: supershape()
// Usage: As Module
// supershape(step, [m1=], [m2=], [n1=], [n2=], [n3=], [a=], [b=], <r=/d=>);
// Usage: With Attachments
// supershape(step, [m1=], [m2=], [n1=], [n2=], [n3=], [a=], [b=], <r=/d=>) { attachments }
// Usage: As Function
// path = supershape(step, [m1=], [m2=], [n1=], [n2=], [n3=], [a=], [b=], <r=/d=>);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle(), oval()
// Description:
// When called as a function, returns a 2D path for the outline of the [Superformula](https://en.wikipedia.org/wiki/Superformula) shape.
// When called as a module, creates a 2D [Superformula](https://en.wikipedia.org/wiki/Superformula) shape.
// Arguments:
// step = The angle step size for sampling the superformula shape. Smaller steps are slower but more accurate.
// m1 = The m1 argument for the superformula. Default: 4.
// m2 = The m2 argument for the superformula. Default: m1.
// n1 = The n1 argument for the superformula. Default: 1.
// n2 = The n2 argument for the superformula. Default: n1.
// n3 = The n3 argument for the superformula. Default: n2.
// a = The a argument for the superformula. Default: 1.
// b = The b argument for the superformula. Default: a.
// r = Radius of the shape. Scale shape to fit in a circle of radius r.
// ---
// d = Diameter of the shape. Scale shape to fit in a circle of diameter d.
// 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):
// supershape(step=0.5,m1=16,m2=16,n1=0.5,n2=0.5,n3=16,r=50);
// Example(2D): Called as Function
// stroke(closed=true, supershape(step=0.5,m1=16,m2=16,n1=0.5,n2=0.5,n3=16,d=100));
// Examples(2D,Med):
// for(n=[2:5]) right(2.5*(n-2)) supershape(m1=4,m2=4,n1=n,a=1,b=2); // Superellipses
// m=[2,3,5,7]; for(i=[0:3]) right(2.5*i) supershape(.5,m1=m[i],n1=1);
// m=[6,8,10,12]; for(i=[0:3]) right(2.7*i) supershape(.5,m1=m[i],n1=1,b=1.5); // m should be even
// m=[1,2,3,5]; for(i=[0:3]) fwd(1.5*i) supershape(m1=m[i],n1=0.4);
// supershape(m1=5, n1=4, n2=1); right(2.5) supershape(m1=5, n1=40, n2=10);
// m=[2,3,5,7]; for(i=[0:3]) right(2.5*i) supershape(m1=m[i], n1=60, n2=55, n3=30);
// n=[0.5,0.2,0.1,0.02]; for(i=[0:3]) right(2.5*i) supershape(m1=5,n1=n[i], n2=1.7);
// supershape(m1=2, n1=1, n2=4, n3=8);
// supershape(m1=7, n1=2, n2=8, n3=4);
// supershape(m1=7, n1=3, n2=4, n3=17);
// supershape(m1=4, n1=1/2, n2=1/2, n3=4);
// supershape(m1=4, n1=4.0,n2=16, n3=1.5, a=0.9, b=9);
// for(i=[1:4]) right(3*i) supershape(m1=i, m2=3*i, n1=2);
// m=[4,6,10]; for(i=[0:2]) right(i*5) supershape(m1=m[i], n1=12, n2=8, n3=5, a=2.7);
// for(i=[-1.5:3:1.5]) right(i*1.5) supershape(m1=2,m2=10,n1=i,n2=1);
// for(i=[1:3],j=[-1,1]) translate([3.5*i,1.5*j])supershape(m1=4,m2=6,n1=i*j,n2=1);
// for(i=[1:3]) right(2.5*i)supershape(step=.5,m1=88, m2=64, n1=-i*i,n2=1,r=1);
// Examples:
// linear_extrude(height=0.3, scale=0) supershape(step=1, m1=6, n1=0.4, n2=0, n3=6);
// linear_extrude(height=5, scale=0) supershape(step=1, b=3, m1=6, n1=3.8, n2=16, n3=10);
function supershape(step=0.5, m1=4, m2, n1=1, n2, n3, a=1, b, r, d,anchor=CENTER, spin=0) =
let(
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,
b = is_def(b) ? b : a,
steps = ceil(360/step),
step = 360/steps,
angs = [for (i = [0:steps]) step*i],
rads = [for (theta = angs) _superformula(theta=theta,m1=m1,m2=m2,n1=n1,n2=n2,n3=n3,a=a,b=b)],
scale = is_def(r) ? r/max(rads) : 1,
path = [for (i = [steps:-1:1]) let(a=angs[i]) scale*rads[i]*[cos(a), sin(a)]]
) reorient(anchor,spin, two_d=true, path=path, p=path);
module supershape(step=0.5,m1=4,m2=undef,n1,n2=undef,n3=undef,a=1,b=undef, r=undef, d=undef, anchor=CENTER, spin=0) {
path = supershape(step=step,m1=m1,m2=m2,n1=n1,n2=n2,n3=n3,a=a,b=b,r=r,d=d);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
// Function&Module: reuleaux_polygon()
// Usage: As Module
// reuleaux_polygon(N, r|d, ...);
// Usage: As Function
// path = reuleaux_polygon(N, r|d, ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: regular_ngon(), pentagon(), hexagon(), octagon()
// Description:
// Creates a 2D Reuleaux Polygon; a constant width shape that is not circular.
// Arguments:
// N = Number of "sides" to the Reuleaux Polygon. Must be an odd positive number. Default: 3
// r = Radius of the shape. Scale shape to fit in a circle of radius r.
// ---
// d = Diameter of the shape. Scale shape to fit in a circle of diameter d.
// 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`
// Extra Anchors:
// "tip0", "tip1", etc. = Each tip has an anchor, pointing outwards.
// Examples(2D):
// reuleaux_polygon(N=3, r=50);
// reuleaux_polygon(N=5, d=100);
// Examples(2D): Standard vector anchors are based on extents
// reuleaux_polygon(N=3, d=50) show_anchors(custom=false);
// Examples(2D): Named anchors exist for the tips
// reuleaux_polygon(N=3, d=50) show_anchors(std=false);
module reuleaux_polygon(N=3, r, d, anchor=CENTER, spin=0) {
assert(N>=3 && (N%2)==1);
r = get_radius(r=r, d=d, dflt=1);
path = reuleaux_polygon(N=N, r=r);
anchors = [
for (i = [0:1:N-1]) let(
ca = 360 - i * 360/N,
cp = polar_to_xy(r, ca)
) named_anchor(str("tip",i), cp, unit(cp,BACK), 0),
];
attachable(anchor,spin, two_d=true, path=path, anchors=anchors) {
polygon(path);
children();
}
}
function reuleaux_polygon(N=3, r, d, anchor=CENTER, spin=0) =
assert(N>=3 && (N%2)==1)
let(
r = get_radius(r=r, d=d, dflt=1),
ssegs = max(3,ceil(segs(r)/N)),
slen = norm(polar_to_xy(r,0)-polar_to_xy(r,180-180/N)),
path = [
for (i = [0:1:N-1]) let(
ca = 180 - (i+0.5) * 360/N,
sa = ca + 180 + (90/N),
ea = ca + 180 - (90/N),
cp = polar_to_xy(r, ca)
) each arc(N=ssegs-1, r=slen, cp=cp, angle=[sa,ea], endpoint=false)
],
anchors = [
for (i = [0:1:N-1]) let(
ca = 360 - i * 360/N,
cp = polar_to_xy(r, ca)
) named_anchor(str("tip",i), cp, unit(cp,BACK), 0),
]
) reorient(anchor,spin, two_d=true, path=path, anchors=anchors, p=path);
// Section: 2D Masking Shapes
// Function&Module: mask2d_roundover()
// Usage: As Module
// mask2d_roundover(r|d, [inset], [excess]);
// Usage: With Attachments
// mask2d_roundover(r|d, [inset], [excess]) { attachments }
// Usage: As Module
// path = mask2d_roundover(r|d, [inset], [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D roundover/bead mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// Arguments:
// r = Radius of the roundover.
// inset = Optional bead inset size. Default: 0
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// d = Diameter of the roundover.
// 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): 2D Roundover Mask
// mask2d_roundover(r=10);
// Example(2D): 2D Bead Mask
// mask2d_roundover(r=10,inset=2);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile([TOP,"Z"],except=[BACK,TOP+LEFT])
// mask2d_roundover(r=10, inset=2);
module mask2d_roundover(r, inset=0, excess=0.01, d, anchor=CENTER,spin=0) {
path = mask2d_roundover(r=r,d=d,excess=excess,inset=inset);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
function mask2d_roundover(r, inset=0, excess=0.01, d, anchor=CENTER,spin=0) =
assert(is_num(r)||is_num(d))
assert(is_undef(excess)||is_num(excess))
assert(is_num(inset)||(is_vector(inset)&&len(inset)==2))
let(
inset = is_list(inset)? inset : [inset,inset],
excess = default(excess,$overlap),
r = get_radius(r=r,d=d,dflt=1),
steps = quantup(segs(r),4)/4,
step = 90/steps,
path = [
[r+inset.x,-excess],
[-excess,-excess],
[-excess, r+inset.y],
for (i=[0:1:steps]) [r,r] + inset + polar_to_xy(r,180+i*step)
]
) reorient(anchor,spin, two_d=true, path=path, extent=false, p=path);
// Function&Module: mask2d_cove()
// Usage: As Module
// mask2d_cove(r|d, [inset], [excess]);
// Usage: With Attachments
// mask2d_cove(r|d, [inset], [excess]) { attachments }
// Usage: As Function
// path = mask2d_cove(r|d, [inset], [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D cove mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// Arguments:
// r = Radius of the cove.
// inset = Optional amount to inset code from corner. Default: 0
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// d = Diameter of the cove.
// 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): 2D Cove Mask
// mask2d_cove(r=10);
// Example(2D): 2D Inset Cove Mask
// mask2d_cove(r=10,inset=3);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile([TOP,"Z"],except=[BACK,TOP+LEFT])
// mask2d_cove(r=10, inset=2);
module mask2d_cove(r, inset=0, excess=0.01, d, anchor=CENTER,spin=0) {
path = mask2d_cove(r=r,d=d,excess=excess,inset=inset);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
function mask2d_cove(r, inset=0, excess=0.01, d, anchor=CENTER,spin=0) =
assert(is_num(r)||is_num(d))
assert(is_undef(excess)||is_num(excess))
assert(is_num(inset)||(is_vector(inset)&&len(inset)==2))
let(
inset = is_list(inset)? inset : [inset,inset],
excess = default(excess,$overlap),
r = get_radius(r=r,d=d,dflt=1),
steps = quantup(segs(r),4)/4,
step = 90/steps,
path = [
[r+inset.x,-excess],
[-excess,-excess],
[-excess, r+inset.y],
for (i=[0:1:steps]) inset + polar_to_xy(r,90-i*step)
]
) reorient(anchor,spin, two_d=true, path=path, p=path);
// Function&Module: mask2d_chamfer()
// Usage: As Module
// mask2d_chamfer(edge, [angle], [inset], [excess]);
// mask2d_chamfer(y, [angle], [inset], [excess]);
// mask2d_chamfer(x, [angle], [inset], [excess]);
// Usage: With Attachments
// mask2d_chamfer(edge, [angle], [inset], [excess]) { attachments }
// Usage: As Function
// path = mask2d_chamfer(edge, [angle], [inset], [excess]);
// path = mask2d_chamfer(y, [angle], [inset], [excess]);
// path = mask2d_chamfer(x, [angle], [inset], [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D chamfer mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// Arguments:
// edge = The length of the edge of the chamfer.
// angle = The angle of the chamfer edge, away from vertical. Default: 45.
// inset = Optional amount to inset code from corner. Default: 0
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// x = The width of the chamfer.
// y = The height of the chamfer.
// 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): 2D Chamfer Mask
// mask2d_chamfer(x=10);
// Example(2D): 2D Chamfer Mask by Width.
// mask2d_chamfer(x=10, angle=30);
// Example(2D): 2D Chamfer Mask by Height.
// mask2d_chamfer(y=10, angle=30);
// Example(2D): 2D Inset Chamfer Mask
// mask2d_chamfer(x=10, inset=2);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile([TOP,"Z"],except=[BACK,TOP+LEFT])
// mask2d_chamfer(x=10, inset=2);
module mask2d_chamfer(edge, angle=45, inset=0, excess=0.01, x, y, anchor=CENTER,spin=0) {
path = mask2d_chamfer(x=x, y=y, edge=edge, angle=angle, excess=excess, inset=inset);
attachable(anchor,spin, two_d=true, path=path, extent=true) {
polygon(path);
children();
}
}
function mask2d_chamfer(edge, angle=45, inset=0, excess=0.01, x, y, anchor=CENTER,spin=0) =
assert(num_defined([x,y,edge])==1)
assert(is_num(first_defined([x,y,edge])))
assert(is_num(angle))
assert(is_undef(excess)||is_num(excess))
assert(is_num(inset)||(is_vector(inset)&&len(inset)==2))
let(
inset = is_list(inset)? inset : [inset,inset],
excess = default(excess,$overlap),
x = !is_undef(x)? x :
!is_undef(y)? adj_ang_to_opp(adj=y,ang=angle) :
hyp_ang_to_opp(hyp=edge,ang=angle),
y = opp_ang_to_adj(opp=x,ang=angle),
path = [
[x+inset.x, -excess],
[-excess, -excess],
[-excess, y+inset.y],
[inset.x, y+inset.y],
[x+inset.x, inset.y]
]
) reorient(anchor,spin, two_d=true, path=path, extent=true, p=path);
// Function&Module: mask2d_rabbet()
// Usage: As Module
// mask2d_rabbet(size, [excess]);
// Usage: With Attachments
// mask2d_rabbet(size, [excess]) { attachments }
// Usage: As Function
// path = mask2d_rabbet(size, [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D rabbet mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// Arguments:
// size = The size of the rabbet, either as a scalar or an [X,Y] list.
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// 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): 2D Rabbet Mask
// mask2d_rabbet(size=10);
// Example(2D): 2D Asymmetrical Rabbet Mask
// mask2d_rabbet(size=[5,10]);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile([TOP,"Z"],except=[BACK,TOP+LEFT])
// mask2d_rabbet(size=10);
module mask2d_rabbet(size, excess=0.01, anchor=CENTER,spin=0) {
path = mask2d_rabbet(size=size, excess=excess);
attachable(anchor,spin, two_d=true, path=path, extent=false) {
polygon(path);
children();
}
}
function mask2d_rabbet(size, excess=0.01, anchor=CENTER,spin=0) =
assert(is_num(size)||(is_vector(size)&&len(size)==2))
assert(is_undef(excess)||is_num(excess))
let(
excess = default(excess,$overlap),
size = is_list(size)? size : [size,size],
path = [
[size.x, -excess],
[-excess, -excess],
[-excess, size.y],
size
]
) reorient(anchor,spin, two_d=true, path=path, extent=false, p=path);
// Function&Module: mask2d_dovetail()
// Usage: As Module
// mask2d_dovetail(edge, [angle], [inset], [shelf], [excess], ...);
// mask2d_dovetail(x=, [angle=], [inset=], [shelf=], [excess=], ...);
// mask2d_dovetail(y=, [angle=], [inset=], [shelf=], [excess=], ...);
// Usage: With Attachments
// mask2d_dovetail(edge, [angle], [inset], [shelf], ...) { attachments }
// Usage: As Function
// path = mask2d_dovetail(edge, [angle], [inset], [shelf], [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D dovetail mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// Arguments:
// edge = The length of the edge of the dovetail.
// angle = The angle of the chamfer edge, away from vertical. Default: 30.
// inset = Optional amount to inset code from corner. Default: 0
// shelf = The extra height to add to the inside corner of the dovetail. Default: 0
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// x = The width of the dovetail.
// y = The height of the dovetail.
// 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): 2D Dovetail Mask
// mask2d_dovetail(x=10);
// Example(2D): 2D Dovetail Mask by Width.
// mask2d_dovetail(x=10, angle=30);
// Example(2D): 2D Dovetail Mask by Height.
// mask2d_dovetail(y=10, angle=30);
// Example(2D): 2D Inset Dovetail Mask
// mask2d_dovetail(x=10, inset=2);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile([TOP,"Z"],except=[BACK,TOP+LEFT])
// mask2d_dovetail(x=10, inset=2);
module mask2d_dovetail(edge, angle=30, inset=0, shelf=0, excess=0.01, x, y, anchor=CENTER, spin=0) {
path = mask2d_dovetail(x=x, y=y, edge=edge, angle=angle, inset=inset, shelf=shelf, excess=excess);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
function mask2d_dovetail(edge, angle=30, inset=0, shelf=0, excess=0.01, x, y, anchor=CENTER, spin=0) =
assert(num_defined([x,y,edge])==1)
assert(is_num(first_defined([x,y,edge])))
assert(is_num(angle))
assert(is_undef(excess)||is_num(excess))
assert(is_num(inset)||(is_vector(inset)&&len(inset)==2))
let(
inset = is_list(inset)? inset : [inset,inset],
excess = default(excess,$overlap),
x = !is_undef(x)? x :
!is_undef(y)? adj_ang_to_opp(adj=y,ang=angle) :
hyp_ang_to_opp(hyp=edge,ang=angle),
y = opp_ang_to_adj(opp=x,ang=angle),
path = [
[inset.x,0],
[-excess, 0],
[-excess, y+inset.y+shelf],
inset+[x,y+shelf],
inset+[x,y],
inset
]
) reorient(anchor,spin, two_d=true, path=path, p=path);
// Function&Module: mask2d_teardrop()
// Usage: As Module
// mask2d_teardrop(r|d, [angle], [excess]);
// Usage: With Attachments
// mask2d_teardrop(r|d, [angle], [excess]) { attachments }
// Usage: As Function
// path = mask2d_teardrop(r|d, [angle], [excess]);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
// Description:
// Creates a 2D teardrop mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be differenced away from the edge of a shape that is in the first (X+Y+) quadrant.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// This is particularly useful to make partially rounded bottoms, that don't need support to print.
// Arguments:
// r = Radius of the rounding.
// angle = The maximum angle from vertical.
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// d = Diameter of the rounding.
// 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): 2D Teardrop Mask
// mask2d_teardrop(r=10);
// Example(2D): Using a Custom Angle
// mask2d_teardrop(r=10,angle=30);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile(BOT)
// mask2d_teardrop(r=10, angle=40);
function mask2d_teardrop(r, angle=45, excess=0.01, d, anchor=CENTER, spin=0) =
assert(is_num(angle))
assert(angle>0 && angle<90)
assert(is_num(excess))
let(
r = get_radius(r=r, d=d, dflt=1),
n = ceil(segs(r) * angle/360),
cp = [r,r],
tp = cp + polar_to_xy(r,180+angle),
bp = [tp.x+adj_ang_to_opp(tp.y,angle), 0],
step = angle/n,
path = [
bp, bp-[0,excess], [-excess,-excess], [-excess,r],
for (i=[0:1:n]) cp+polar_to_xy(r,180+i*step)
]
) reorient(anchor,spin, two_d=true, path=path, p=path);
module mask2d_teardrop(r, angle=45, excess=0.01, d, anchor=CENTER, spin=0) {
path = mask2d_teardrop(r=r, d=d, angle=angle, excess=excess);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
// Function&Module: mask2d_ogee()
// Usage: As Module
// mask2d_ogee(pattern, [excess], ...);
// Usage: With Attachments
// mask2d_ogee(pattern, [excess], ...) { attachments }
// Usage: As Function
// path = mask2d_ogee(pattern, [excess], ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable, Masks (2D)
// See Also: corner_profile(), edge_profile(), face_profile()
//
// Description:
// Creates a 2D Ogee mask shape that is useful for extruding into a 3D mask for a 90º edge.
// This 2D mask is designed to be `difference()`d away from the edge of a shape that is in the first (X+Y+) quadrant.
// Since there are a number of shapes that fall under the name ogee, the shape of this mask is given as a pattern.
// Patterns are given as TYPE, VALUE pairs. ie: `["fillet",10, "xstep",2, "step",[5,5], ...]`. See Patterns below.
// If called as a function, this just returns a 2D path of the outline of the mask shape.
// .
// ### Patterns
// .
// Type | Argument | Description
// -------- | --------- | ----------------
// "step" | [x,y] | Makes a line to a point `x` right and `y` down.
// "xstep" | dist | Makes a `dist` length line towards X+.
// "ystep" | dist | Makes a `dist` length line towards Y-.
// "round" | radius | Makes an arc that will mask a roundover.
// "fillet" | radius | Makes an arc that will mask a fillet.
//
// Arguments:
// pattern = A list of pattern pieces to describe the Ogee.
// excess = Extra amount of mask shape to creates on the X- and Y- sides of the shape. Default: 0.01
// ---
// 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): 2D Ogee Mask
// mask2d_ogee([
// "xstep",1, "ystep",1, // Starting shoulder.
// "fillet",5, "round",5, // S-curve.
// "ystep",1, "xstep",1 // Ending shoulder.
// ]);
// Example: Masking by Edge Attachment
// diff("mask")
// cube([50,60,70],center=true)
// edge_profile(TOP)
// mask2d_ogee([
// "xstep",1, "ystep",1, // Starting shoulder.
// "fillet",5, "round",5, // S-curve.
// "ystep",1, "xstep",1 // Ending shoulder.
// ]);
module mask2d_ogee(pattern, excess=0.01, anchor=CENTER,spin=0) {
path = mask2d_ogee(pattern, excess=excess);
attachable(anchor,spin, two_d=true, path=path) {
polygon(path);
children();
}
}
function mask2d_ogee(pattern, excess=0.01, anchor=CENTER, spin=0) =
assert(is_list(pattern))
assert(len(pattern)>0)
assert(len(pattern)%2==0,"pattern must be a list of TYPE, VAL pairs.")
assert(all([for (i = idx(pattern,step=2)) in_list(pattern[i],["step","xstep","ystep","round","fillet"])]))
let(
excess = default(excess,$overlap),
x = concat([0], cumsum([
for (i=idx(pattern,step=2)) let(
type = pattern[i],
val = pattern[i+1]
) (
type=="step"? val.x :
type=="xstep"? val :
type=="round"? val :
type=="fillet"? val :
0
)
])),
y = concat([0], cumsum([
for (i=idx(pattern,step=2)) let(
type = pattern[i],
val = pattern[i+1]
) (
type=="step"? val.y :
type=="ystep"? val :
type=="round"? val :
type=="fillet"? val :
0
)
])),
tot_x = last(x),
tot_y = last(y),
data = [
for (i=idx(pattern,step=2)) let(
type = pattern[i],
val = pattern[i+1],
pt = [x[i/2], tot_y-y[i/2]] + (
type=="step"? [val.x,-val.y] :
type=="xstep"? [val,0] :
type=="ystep"? [0,-val] :
type=="round"? [val,0] :
type=="fillet"? [0,-val] :
[0,0]
)
) [type, val, pt]
],
path = [
[tot_x,-excess],
[-excess,-excess],
[-excess,tot_y],
for (pat = data) each
pat[0]=="step"? [pat[2]] :
pat[0]=="xstep"? [pat[2]] :
pat[0]=="ystep"? [pat[2]] :
let(
r = pat[1],
steps = segs(abs(r)),
step = 90/steps
) [
for (i=[0:1:steps]) let(
a = pat[0]=="round"? (180+i*step) : (90-i*step)
) pat[2] + abs(r)*[cos(a),sin(a)]
]
],
path2 = deduplicate(path)
) reorient(anchor,spin, two_d=true, path=path2, p=path2);
// Section: Debugging polygons
// Module: debug_polygon()
// Usage:
// debug_polygon(points, paths, [convexity=], [size=]);
// Description:
// A drop-in replacement for `polygon()` that renders and labels the path points.
// Arguments:
// points = The array of 2D polygon vertices.
// paths = The path connections between the vertices.
// ---
// convexity = The max number of walls a ray can pass through the given polygon paths.
// size = The base size of the line and labels.
// Example(Big2D):
// debug_polygon(
// points=concat(
// regular_ngon(or=10, n=8),
// regular_ngon(or=8, n=8)
// ),
// paths=[
// [for (i=[0:7]) i],
// [for (i=[15:-1:8]) i]
// ]
// );
module debug_polygon(points, paths, convexity=2, size=1)
{
paths = is_undef(paths)? [[for (i=[0:1:len(points)-1]) i]] :
is_num(paths[0])? [paths] :
paths;
echo(points=points);
echo(paths=paths);
linear_extrude(height=0.01, convexity=convexity, center=true) {
polygon(points=points, paths=paths, convexity=convexity);
}
for (i = [0:1:len(points)-1]) {
color("red") {
up(0.2) {
translate(points[i]) {
linear_extrude(height=0.1, convexity=10, center=true) {
text(text=str(i), size=size, halign="center", valign="center");
}
}
}
}
}
for (j = [0:1:len(paths)-1]) {
path = paths[j];
translate(points[path[0]]) {
color("cyan") up(0.1) cylinder(d=size*1.5, h=0.01, center=false, $fn=12);
}
translate(points[path[len(path)-1]]) {
color("pink") up(0.11) cylinder(d=size*1.5, h=0.01, center=false, $fn=4);
}
for (i = [0:1:len(path)-1]) {
midpt = (points[path[i]] + points[path[(i+1)%len(path)]])/2;
color("blue") {
up(0.2) {
translate(midpt) {
linear_extrude(height=0.1, convexity=10, center=true) {
text(text=str(chr(65+j),i), size=size/2, halign="center", valign="center");
}
}
}
}
}
}
}
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap