From c71112347e0ad738d2a4794fd531b2c7a2cdcb08 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Mon, 5 Oct 2020 17:40:32 -0400 Subject: [PATCH 1/7] minor doc fixes and bugfix for closed path tangents with path_sweep --- shapes2d.scad | 9 ++++----- skin.scad | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/shapes2d.scad b/shapes2d.scad index 4c11efd..2f739f1 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -472,7 +472,7 @@ function _normal_segment(p1,p2) = // Function: turtle() // Usage: -// turtle(commands, [state], [return_state]) +// turtle(commands, [state], [full_state], [repeat]) // Description: // Use a sequence of turtle graphics commands to generate a path. The parameter `commands` is a list of // turtle commands and optional parameters for each command. The turtle state has a position, movement direction, @@ -481,9 +481,8 @@ function _normal_segment(p1,p2) = // the computed turtle path. If you set `full_state` to true then it instead returns the full turtle state. // You can invoke `turtle` again with this full state to continue the turtle path where you left off. // . -// The turtle state is a list with three entries: the path constructed so far, the current step as a 2-vector, and the current default angle. -// . -// For the list below, `dist` is the current movement distance. +// The turtle state is a list with three entries: the path constructed so far, the current step as a 2-vector, the current default angle, +// and the current arcsteps setting. // . // Commands | Arguments | What it does // ------------ | ------------------ | ------------------------------- @@ -613,7 +612,7 @@ function _turtle(commands, state, full_state, index=0) = ) : ( full_state ? state : state[0] ); -// Turtle state: state = [path, step_vector, default angle] +// Turtle state: state = [path, step_vector, default angle, default arcsteps] function _turtle_command(command, parm, parm2, state, index) = command == "repeat"? diff --git a/skin.scad b/skin.scad index 87ae8d4..7122b10 100644 --- a/skin.scad +++ b/skin.scad @@ -1184,7 +1184,7 @@ function path_sweep(shape, path, method="incremental", normal, closed=false, twi assert(is_undef(normal) || (is_vector(normal) && len(normal)==3) || (is_path(normal) && len(normal)==len(path) && len(normal[0])==3), "Invalid normal specified") assert(is_undef(tangent) || (is_path(tangent) && len(tangent)==len(path) && len(tangent[0])==3), "Invalid tangent specified") let( - tangents = is_undef(tangent) ? path_tangents(path) : [for(t=tangent) unit(t)], + tangents = is_undef(tangent) ? path_tangents(path,closed=closed) : [for(t=tangent) unit(t)], normal = is_path(normal) ? [for(n=normal) unit(n)] : is_def(normal) ? unit(normal) : method =="incremental" && abs(tangents[0].z) > 1/sqrt(2) ? BACK : UP, From 0cd34406a365bb02a21dc38b5e0912e8c9869f28 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Tue, 6 Oct 2020 01:41:39 -0700 Subject: [PATCH 2/7] Documented looping helpers. --- common.scad | 73 +++++++++++++++++++++++++++++++++++++++++++++++++--- version.scad | 2 +- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/common.scad b/common.scad index 5a22b61..49af95d 100644 --- a/common.scad +++ b/common.scad @@ -458,10 +458,75 @@ module shape_compare(eps=1/1024) { } -function loop_start() = 0; -function loop_done(x) = x==1; -function looping(x) = x<2; -function loop_next(x,b) = x>=1? 2 : (b? 0 : 1); +// Section: Looping Helpers +// You can use a list comprehension with a C-style for loop to iteratively make a calculation. +// The syntax is: `[for (INIT; CONDITION; NEXT) RETVAL]` where: +// - INIT is zero or more `let()` style assignments that are evaluated exactly one time, before the first loop. +// - CONDITION is an expression evaluated at the start of each loop. If true, continues with the loop. +// - RETVAL is an expression that returns a list item for each loop. +// - NEXT is one or more `let()` style assignments that is evaluated at the end of each loop. +// Since the INIT phase is only run once, and the CONDITION and RETVAL expressions cannot update +// variables, that means that only the NEXT phase can be used for iterative calculations. +// Unfortunately, the NEXT phase runs *after* the RETVAL expression, which means that you need +// to run the loop one extra time to return the final value. This tends to make the loop code +// look rather ugly. The `looping()`, `loop_next()` and `loop_done()` functions +// can make this somewhat more legible. +// Example(NORENDER): +// function flat_sum(l) = [ +// for ( +// i = 0, +// total = 0, +// state = 0; +// +// looping(state); +// +// state = loop_next(state, i < len(l)), +// total = total + +// !looping(state) ? 0 : +// let( x = l[i] ) +// is_list(x) ? flat_sum(x) : x, +// i = i + 1 +// ) if (loop_done(state)) total; +// ].x; + + +// Function: looping() +// Usage: +// looping(state) +// Description: +// Returns true if the `state` value indicates the loop is still progressing. +// This is useful when using C-style for loops to iteratively calculate a value. +// Used with `loop_next()` and `loop_done()`. See [Looping Helpers](#looping-helpers) for an example. +// Arguments: +// state = The loop state value. +function looping(state) = state<2; + + +// Function: loop_next() +// Usage: +// state = loop_next(state, continue) +// Description: +// Given the current `state`, and a boolean `continue` that indicates if the loop should still be +// continuing, returns the updated state value for the the next loop. +// This is useful when using C-style for loops to iteratively calculate a value. +// Used with `looping()` and `loop_done()`. See [Looping Helpers](#looping-helpers) for an example. +// Arguments: +// state = The loop state value. +// continue = A boolean value indicating whether the current loop should progress. +function loop_next(state, continue) = + state>=1? 2 : (continue? 0 : 1); + + +// Function: loop_done() +// Usage: +// loop_done(state) +// Description: +// Returns true if the `state` value indicates the loop is finishing. +// This is useful when using C-style for loops to iteratively calculate a value. +// Used with `looping()` and `loop_next()`. See [Looping Helpers](#looping-helpers) for an example. +// Arguments: +// state = The loop state value. +function loop_done(state) = state==1; // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/version.scad b/version.scad index 86ef763..8d89633 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,440]; +BOSL_VERSION = [2,0,441]; // Section: BOSL Library Version Functions From 8b922f7a153c010990dffd5216688e0ff50aff79 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Tue, 6 Oct 2020 13:48:14 -0700 Subject: [PATCH 3/7] Fixed Looping Helpers docs formatting. --- common.scad | 5 ++++- version.scad | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/common.scad b/common.scad index 49af95d..d847bd5 100644 --- a/common.scad +++ b/common.scad @@ -460,18 +460,20 @@ module shape_compare(eps=1/1024) { // Section: Looping Helpers // You can use a list comprehension with a C-style for loop to iteratively make a calculation. +// . // The syntax is: `[for (INIT; CONDITION; NEXT) RETVAL]` where: // - INIT is zero or more `let()` style assignments that are evaluated exactly one time, before the first loop. // - CONDITION is an expression evaluated at the start of each loop. If true, continues with the loop. // - RETVAL is an expression that returns a list item for each loop. // - NEXT is one or more `let()` style assignments that is evaluated at the end of each loop. +// . // Since the INIT phase is only run once, and the CONDITION and RETVAL expressions cannot update // variables, that means that only the NEXT phase can be used for iterative calculations. // Unfortunately, the NEXT phase runs *after* the RETVAL expression, which means that you need // to run the loop one extra time to return the final value. This tends to make the loop code // look rather ugly. The `looping()`, `loop_next()` and `loop_done()` functions // can make this somewhat more legible. -// Example(NORENDER): +// ```openscad // function flat_sum(l) = [ // for ( // i = 0, @@ -488,6 +490,7 @@ module shape_compare(eps=1/1024) { // i = i + 1 // ) if (loop_done(state)) total; // ].x; +// ``` // Function: looping() diff --git a/version.scad b/version.scad index 8d89633..8b8ddcf 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,441]; +BOSL_VERSION = [2,0,442]; // Section: BOSL Library Version Functions From b22c5a0cfe389f953c9157206f79bb45d68f4351 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Tue, 6 Oct 2020 18:48:39 -0700 Subject: [PATCH 4/7] Bugfixes for looping helpers. --- common.scad | 27 ++++++++++++++------------- version.scad | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/common.scad b/common.scad index d847bd5..0a08f5d 100644 --- a/common.scad +++ b/common.scad @@ -471,7 +471,7 @@ module shape_compare(eps=1/1024) { // variables, that means that only the NEXT phase can be used for iterative calculations. // Unfortunately, the NEXT phase runs *after* the RETVAL expression, which means that you need // to run the loop one extra time to return the final value. This tends to make the loop code -// look rather ugly. The `looping()`, `loop_next()` and `loop_done()` functions +// look rather ugly. The `looping()`, `loop_while()` and `loop_done()` functions // can make this somewhat more legible. // ```openscad // function flat_sum(l) = [ @@ -482,9 +482,9 @@ module shape_compare(eps=1/1024) { // // looping(state); // -// state = loop_next(state, i < len(l)), +// state = loop_while(state, i < len(l)), // total = total + -// !looping(state) ? 0 : +// loop_done(state) ? 0 : // let( x = l[i] ) // is_list(x) ? flat_sum(x) : x, // i = i + 1 @@ -497,27 +497,28 @@ module shape_compare(eps=1/1024) { // Usage: // looping(state) // Description: -// Returns true if the `state` value indicates the loop is still progressing. +// Returns true if the `state` value indicates the current loop should continue. // This is useful when using C-style for loops to iteratively calculate a value. -// Used with `loop_next()` and `loop_done()`. See [Looping Helpers](#looping-helpers) for an example. +// Used with `loop_while()` and `loop_done()`. See [Looping Helpers](#5-looping-helpers) for an example. // Arguments: // state = The loop state value. -function looping(state) = state<2; +function looping(state) = state < 2; -// Function: loop_next() +// Function: loop_while() // Usage: -// state = loop_next(state, continue) +// state = loop_while(state, continue) // Description: // Given the current `state`, and a boolean `continue` that indicates if the loop should still be // continuing, returns the updated state value for the the next loop. // This is useful when using C-style for loops to iteratively calculate a value. -// Used with `looping()` and `loop_done()`. See [Looping Helpers](#looping-helpers) for an example. +// Used with `looping()` and `loop_done()`. See [Looping Helpers](#5-looping-helpers) for an example. // Arguments: // state = The loop state value. // continue = A boolean value indicating whether the current loop should progress. -function loop_next(state, continue) = - state>=1? 2 : (continue? 0 : 1); +function loop_while(state, continue) = + state > 0 ? 2 : + continue ? 0 : 1; // Function: loop_done() @@ -526,10 +527,10 @@ function loop_next(state, continue) = // Description: // Returns true if the `state` value indicates the loop is finishing. // This is useful when using C-style for loops to iteratively calculate a value. -// Used with `looping()` and `loop_next()`. See [Looping Helpers](#looping-helpers) for an example. +// Used with `looping()` and `loop_while()`. See [Looping Helpers](#5-looping-helpers) for an example. // Arguments: // state = The loop state value. -function loop_done(state) = state==1; +function loop_done(state) = state > 0; // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/version.scad b/version.scad index 8b8ddcf..a2f93d2 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,442]; +BOSL_VERSION = [2,0,443]; // Section: BOSL Library Version Functions From a36eec529793b01829c41374d62b6ca712e9ab0e Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 7 Oct 2020 16:53:52 -0700 Subject: [PATCH 5/7] 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 From 29ed64974057cdd53b1760cf207436ad757e2303 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 7 Oct 2020 22:50:05 -0700 Subject: [PATCH 6/7] Added angle= and shift= to trapezoid() --- geometry.scad | 12 ++++++------ shapes2d.scad | 46 ++++++++++++++++++++++++++++++++++++++-------- version.scad | 2 +- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/geometry.scad b/geometry.scad index b95a4e9..6a69e11 100644 --- a/geometry.scad +++ b/geometry.scad @@ -573,7 +573,7 @@ function hyp_opp_to_adj(hyp,opp) = // adj = hyp_ang_to_adj(8,60); // Returns: 4 function hyp_ang_to_adj(hyp,ang) = assert(is_finite(hyp) && hyp>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) hyp*cos(ang); @@ -590,7 +590,7 @@ function hyp_ang_to_adj(hyp,ang) = // adj = opp_ang_to_adj(8,30); // Returns: 4 function opp_ang_to_adj(opp,ang) = assert(is_finite(opp) && opp>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) opp/tan(ang); @@ -622,7 +622,7 @@ function hyp_adj_to_opp(hyp,adj) = // opp = hyp_ang_to_opp(8,30); // Returns: 4 function hyp_ang_to_opp(hyp,ang) = assert(is_finite(hyp)&&hyp>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) hyp*sin(ang); @@ -638,7 +638,7 @@ function hyp_ang_to_opp(hyp,ang) = // opp = adj_ang_to_opp(8,45); // Returns: 8 function adj_ang_to_opp(adj,ang) = assert(is_finite(adj)&&adj>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) adj*tan(ang); @@ -670,7 +670,7 @@ function adj_opp_to_hyp(adj,opp) = // hyp = adj_ang_to_hyp(4,60); // Returns: 8 function adj_ang_to_hyp(adj,ang) = assert(is_finite(adj) && adj>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) adj/cos(ang); @@ -686,7 +686,7 @@ function adj_ang_to_hyp(adj,ang) = // hyp = opp_ang_to_hyp(4,30); // Returns: 8 function opp_ang_to_hyp(opp,ang) = assert(is_finite(opp) && opp>=0, "Triangle side length should be a positive number." ) - assert(is_finite(ang) && ang>0 && ang<90, "The angle should be an acute angle." ) + assert(is_finite(ang) && ang>-90 && ang<90, "The angle should be an acute angle." ) opp/sin(ang); diff --git a/shapes2d.scad b/shapes2d.scad index 5733870..23a6430 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -1200,26 +1200,56 @@ module octagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, // 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 // 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=20, 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): Called as Function // stroke(closed=true, trapezoid(h=30, w1=40, w2=20)); -function trapezoid(h, w1, w2, anchor=CENTER, spin=0) = +function trapezoid(h, w1, w2, angle, shift=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)) let( - path = [[w1/2,-h/2], [-w1/2,-h/2], [-w2/2,h/2], [w2/2,h/2]] - ) reorient(anchor,spin, two_d=true, size=[w1,h], size2=w2, p=path); + 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), + path = [[w1/2,-h/2], [-w1/2,-h/2], [-w2/2+shift,h/2], [w2/2+shift,h/2]] + ) + assert(w1>=0 && w2>=0 && h>0, "Degenerate trapezoid geometry.") + reorient(anchor,spin, two_d=true, size=[w1,h], size2=w2, p=path); -module trapezoid(h, w1, w2, anchor=CENTER, spin=0) { - path = [[w1/2,-h/2], [-w1/2,-h/2], [-w2/2,h/2], [w2/2,h/2]]; - attachable(anchor,spin, two_d=true, size=[w1,h], size2=w2) { - polygon(path); - children(); +module trapezoid(h, w1, w2, angle, shift=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)); + union() { + 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."); + path = [[w1/2,-h/2], [-w1/2,-h/2], [-w2/2+shift,h/2], [w2/2+shift,h/2]]; + attachable(anchor,spin, two_d=true, size=[w1,h], size2=w2) { + polygon(path); + children(); + } } } diff --git a/version.scad b/version.scad index 736efd1..dec55e9 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,444]; +BOSL_VERSION = [2,0,445]; // Section: BOSL Library Version Functions From 8451f23b7c4b5668ca842272e597acd2ec459a8f Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 7 Oct 2020 23:03:09 -0700 Subject: [PATCH 7/7] Fix trapezoid example. --- shapes2d.scad | 2 +- version.scad | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shapes2d.scad b/shapes2d.scad index 23a6430..8906650 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -1208,7 +1208,7 @@ module octagon(r, d, or, od, ir, id, side, rounding=0, realign=false, align_tip, // 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=20, angle=30); +// 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); diff --git a/version.scad b/version.scad index dec55e9..89757d8 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,445]; +BOSL_VERSION = [2,0,446]; // Section: BOSL Library Version Functions