From a36eec529793b01829c41374d62b6ca712e9ab0e Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 7 Oct 2020 16:53:52 -0700 Subject: [PATCH] Added align_tip/align_side to regular_ngon()/star(). --- shapes2d.scad | 169 +++++++++++++++++++++++++++++++++++++------------- version.scad | 2 +- 2 files changed, 128 insertions(+), 43 deletions(-) diff --git a/shapes2d.scad b/shapes2d.scad index c337fd0..6a700d3 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -910,6 +910,8 @@ function oval(r, d, realign=false, circum=false, anchor=CENTER, spin=0) = // 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: @@ -925,11 +927,22 @@ function oval(r, d, realign=false, circum=false, anchor=CENTER, spin=0) = // 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, anchor=CENTER, spin=0) = +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, @@ -940,13 +953,19 @@ function regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false 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), - path = rounding==0? oval(r=r, realign=realign, $fn=n) : ( + 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 - (realign? 180/n : 0), + 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) @@ -955,13 +974,15 @@ function regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false path3 = polygon_shift(path2,maxx_idx) ) path3 ), - anchors = !is_string(anchor)? [] : [ + path = apply(mat, path4), + anchors = !is_undef(_anchs) ? _anchs : + !is_string(anchor)? [] : [ for (i = [0:1:n-1]) let( - a1 = 360 - i*360/n - (realign? 180/n : 0), + a1 = 360 - i*360/n, a2 = a1 - 360/n, - p1 = polar_to_xy(r,a1), - p2 = polar_to_xy(r,a2), - tipp = polar_to_xy(r-inset+rounding,a1), + 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 [ anchorpt(str("tip",i), tipp, unit(tipp,BACK), 0), @@ -971,28 +992,33 @@ function regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false ) 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, anchor=CENTER, spin=0) { +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."); - path = regular_ngon(n=n, r=r, rounding=rounding, realign=realign); + 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 - (realign? 180/n : 0), + a1 = 360 - i*360/n, a2 = a1 - 360/n, - p1 = polar_to_xy(r,a1), - p2 = polar_to_xy(r,a2), - tipp = polar_to_xy(r-inset+rounding,a1), + 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 [ anchorpt(str("tip",i), tipp, unit(tipp,BACK), 0), anchorpt(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(); @@ -1018,6 +1044,8 @@ module regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false, // 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: @@ -1033,16 +1061,24 @@ module regular_ngon(n=6, r, d, or, od, ir, id, side, rounding=0, realign=false, // 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, 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, anchor=anchor, spin=spin); +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, 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, anchor=anchor, spin=spin) children(); +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() @@ -1061,6 +1097,8 @@ module pentagon(r, d, or, od, ir, id, side, rounding=0, realign=false, anchor=CE // 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: @@ -1076,16 +1114,24 @@ module pentagon(r, d, or, od, ir, id, side, rounding=0, realign=false, anchor=CE // 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, 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, anchor=anchor, spin=spin); +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, 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, anchor=anchor, spin=spin) children(); +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() @@ -1104,6 +1150,8 @@ module hexagon(r, d, or, od, ir, id, side, rounding=0, realign=false, anchor=CEN // 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: @@ -1119,16 +1167,24 @@ module hexagon(r, d, or, od, ir, id, side, rounding=0, realign=false, anchor=CEN // 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, 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, anchor=anchor, spin=spin); +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, 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, anchor=anchor, spin=spin) children(); +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(); @@ -1299,12 +1355,14 @@ module glued_circles(r, d, spread=10, tangent=30, anchor=CENTER, spin=0) { // 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. -// "corner0" ... "corner4" = The inside corner between each tip has an anchor, pointing outwards. -// "midpt0" ... "midpt4" = The center-point between each pair or tips 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); @@ -1312,9 +1370,20 @@ module glued_circles(r, d, spread=10, tangent=30, anchor=CENTER, spin=0) { // 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, d, or, od, ir, id, step, realign=false, anchor=CENTER, spin=0) = +function star(n, r, d, or, od, ir, 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") let( r = get_radius(r1=or, d1=od, r=r, d=d), count = num_defined([ir,id,step]), @@ -1324,48 +1393,64 @@ function star(n, r, d, or, od, ir, id, step, realign=false, anchor=CENTER, spin= assert(count==1, "Must specify exactly one of ir, id, step") assert(stepOK, str("Parameter 'step' must be between 2 and ",floor(n/2)," for ",n," point star")) 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, - path = [for(i=[2*n:-1:1]) let(theta=180*i/n+offset, radius=(i%2)?ir:r) radius*[cos(theta), sin(theta)]], - anchors = !is_string(anchor)? [] : [ + 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 - (realign? 180/n : 0), + a1 = 360 - i*360/n, a2 = a1 - 180/n, a3 = a1 - 360/n, - p1 = polar_to_xy(r,a1), - p2 = polar_to_xy(ir,a2), - p3 = polar_to_xy(r,a3), + 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 [ anchorpt(str("tip",i), p1, unit(p1,BACK), 0), - anchorpt(str("corner",i), p2, unit(p2,BACK), 0), + anchorpt(str("pit",i), p2, unit(p2,BACK), 0), anchorpt(str("midpt",i), pos, unit(pos,BACK), 0), ] ] ) reorient(anchor,spin, two_d=true, path=path, p=path, anchors=anchors); -module star(n, r, d, or, od, ir, id, step, realign=false, anchor=CENTER, spin=0) { +module star(n, r, d, or, od, ir, 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); - path = star(n=n, r=r, ir=ir, realign=realign); + 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 = polar_to_xy(r,a1), - p2 = polar_to_xy(ir,a2), - p3 = polar_to_xy(r,a3), + 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 [ anchorpt(str("tip",i), p1, unit(p1,BACK), 0), - anchorpt(str("corner",i), p2, unit(p2,BACK), 0), + anchorpt(str("pit",i), p2, unit(p2,BACK), 0), anchorpt(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(); diff --git a/version.scad b/version.scad index a2f93d2..736efd1 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,443]; +BOSL_VERSION = [2,0,444]; // Section: BOSL Library Version Functions