From 85bdeb4d89d498730a5ab1b2ca6e675dad967b96 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Fri, 14 Jan 2022 20:31:02 -0500 Subject: [PATCH] colors and 2d rounding flee mutators.scad --- mutators.scad | 173 -------------------------------------------------- shapes2d.scad | 71 +++++++++++++++++++++ std.scad | 1 + 3 files changed, 72 insertions(+), 173 deletions(-) diff --git a/mutators.scad b/mutators.scad index c0fc682..895e7f4 100644 --- a/mutators.scad +++ b/mutators.scad @@ -443,72 +443,6 @@ module minkowski_difference(planar=false) { } -// Module: round2d() -// Usage: -// round2d(r) ... -// round2d(or) ... -// round2d(ir) ... -// round2d(or, ir) ... -// Description: -// Rounds arbitrary 2D objects. Giving `r` rounds all concave and convex corners. Giving just `ir` -// rounds just concave corners. Giving just `or` rounds convex corners. Giving both `ir` and `or` -// can let you round to different radii for concave and convex corners. The 2D object must not have -// any parts narrower than twice the `or` radius. Such parts will disappear. -// Arguments: -// r = Radius to round all concave and convex corners to. -// or = Radius to round only outside (convex) corners to. Use instead of `r`. -// ir = Radius to round only inside (concave) corners to. Use instead of `r`. -// Examples(2D): -// round2d(r=10) {square([40,100], center=true); square([100,40], center=true);} -// round2d(or=10) {square([40,100], center=true); square([100,40], center=true);} -// round2d(ir=10) {square([40,100], center=true); square([100,40], center=true);} -// round2d(or=16,ir=8) {square([40,100], center=true); square([100,40], center=true);} -module round2d(r, or, ir) -{ - or = get_radius(r1=or, r=r, dflt=0); - ir = get_radius(r1=ir, r=r, dflt=0); - offset(or) offset(-ir-or) offset(delta=ir,chamfer=true) children(); -} - - -// Module: shell2d() -// Usage: -// shell2d(thickness, [or], [ir], [fill], [round]) -// Description: -// Creates a hollow shell from 2D children, with optional rounding. -// Arguments: -// thickness = Thickness of the shell. Positive to expand outward, negative to shrink inward, or a two-element list to do both. -// or = Radius to round corners on the outside of the shell. If given a list of 2 radii, [CONVEX,CONCAVE], specifies the radii for convex and concave corners separately. Default: 0 (no outside rounding) -// ir = Radius to round corners on the inside of the shell. If given a list of 2 radii, [CONVEX,CONCAVE], specifies the radii for convex and concave corners separately. Default: 0 (no inside rounding) -// Examples(2D): -// shell2d(10) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(-10) {square([40,100], center=true); square([100,40], center=true);} -// shell2d([-10,10]) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,or=10) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,ir=10) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,or=[10,0]) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,or=[0,10]) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,ir=[10,0]) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(10,ir=[0,10]) {square([40,100], center=true); square([100,40], center=true);} -// shell2d(8,or=[16,8],ir=[16,8]) {square([40,100], center=true); square([100,40], center=true);} -module shell2d(thickness, or=0, ir=0) -{ - thickness = is_num(thickness)? ( - thickness<0? [thickness,0] : [0,thickness] - ) : (thickness[0]>thickness[1])? ( - [thickness[1],thickness[0]] - ) : thickness; - orad = is_finite(or)? [or,or] : or; - irad = is_finite(ir)? [ir,ir] : ir; - difference() { - round2d(or=orad[0],ir=orad[1]) - offset(delta=thickness[1]) - children(); - round2d(or=irad[1],ir=irad[0]) - offset(delta=thickness[0]) - children(); - } -} // Module: offset3d() @@ -579,111 +513,4 @@ module round3d(r, or, ir, size=100) -////////////////////////////////////////////////////////////////////// -// Section: Colors -////////////////////////////////////////////////////////////////////// - -// Function&Module: HSL() -// Usage: -// HSL(h,[s],[l],[a]) ... -// rgb = HSL(h,[s],[l]); -// Description: -// When called as a function, returns the [R,G,B] color for the given hue `h`, saturation `s`, and lightness `l` from the HSL colorspace. -// When called as a module, sets the color to the given hue `h`, saturation `s`, and lightness `l` from the HSL colorspace. -// Arguments: -// h = The hue, given as a value between 0 and 360. 0=red, 60=yellow, 120=green, 180=cyan, 240=blue, 300=magenta. -// s = The saturation, given as a value between 0 and 1. 0 = grayscale, 1 = vivid colors. Default: 1 -// l = The lightness, between 0 and 1. 0 = black, 0.5 = bright colors, 1 = white. Default: 0.5 -// a = When called as a module, specifies the alpha channel as a value between 0 and 1. 0 = fully transparent, 1=opaque. Default: 1 -// Example: -// HSL(h=120,s=1,l=0.5) sphere(d=60); -// Example: -// rgb = HSL(h=270,s=0.75,l=0.6); -// color(rgb) cube(60, center=true); -function HSL(h,s=1,l=0.5) = - let( - h=posmod(h,360) - ) [ - for (n=[0,8,4]) let( - k=(n+h/30)%12 - ) l - s*min(l,1-l)*max(min(k-3,9-k,1),-1) - ]; - -module HSL(h,s=1,l=0.5,a=1) color(HSL(h,s,l),a) children(); - - -// Function&Module: HSV() -// Usage: -// HSV(h,[s],[v],[a]) ... -// rgb = HSV(h,[s],[v]); -// Description: -// When called as a function, returns the [R,G,B] color for the given hue `h`, saturation `s`, and value `v` from the HSV colorspace. -// When called as a module, sets the color to the given hue `h`, saturation `s`, and value `v` from the HSV colorspace. -// Arguments: -// h = The hue, given as a value between 0 and 360. 0=red, 60=yellow, 120=green, 180=cyan, 240=blue, 300=magenta. -// s = The saturation, given as a value between 0 and 1. 0 = grayscale, 1 = vivid colors. Default: 1 -// v = The value, between 0 and 1. 0 = darkest black, 1 = bright. Default: 1 -// a = When called as a module, specifies the alpha channel as a value between 0 and 1. 0 = fully transparent, 1=opaque. Default: 1 -// Example: -// HSV(h=120,s=1,v=1) sphere(d=60); -// Example: -// rgb = HSV(h=270,s=0.75,v=0.9); -// color(rgb) cube(60, center=true); -function HSV(h,s=1,v=1) = - assert(s>=0 && s<=1) - assert(v>=0 && v<=1) - let( - h = posmod(h,360), - c = v * s, - hprime = h/60, - x = c * (1- abs(hprime % 2 - 1)), - rgbprime = hprime <=1 ? [c,x,0] - : hprime <=2 ? [x,c,0] - : hprime <=3 ? [0,c,x] - : hprime <=4 ? [0,x,c] - : hprime <=5 ? [x,0,c] - : hprime <=6 ? [c,0,x] - : [0,0,0], - m=v-c - ) - rgbprime+[m,m,m]; - -module HSV(h,s=1,v=1,a=1) color(HSV(h,s,v),a) children(); - - -// Module: rainbow() -// Usage: -// rainbow(list) ... -// Description: -// Iterates the list, displaying children in different colors for each list item. -// This is useful for debugging lists of paths and such. -// Arguments: -// list = The list of items to iterate through. -// stride = Consecutive colors stride around the color wheel divided into this many parts. -// maxhues = max number of hues to use (to prevent lots of indistinguishable hues) -// shuffle = if true then shuffle the hues in a random order. Default: false -// seed = seed to use for shuffle -// Side Effects: -// Sets the color to progressive values along the ROYGBIV spectrum for each item. -// Sets `$idx` to the index of the current item in `list` that we want to show. -// Sets `$item` to the current item in `list` that we want to show. -// Example(2D): -// rainbow(["Foo","Bar","Baz"]) fwd($idx*10) text(text=$item,size=8,halign="center",valign="center"); -// Example(2D): -// rgn = [circle(d=45,$fn=3), circle(d=75,$fn=4), circle(d=50)]; -// rainbow(rgn) stroke($item, closed=true); -module rainbow(list, stride=1, maxhues, shuffle=false, seed) -{ - ll = len(list); - maxhues = first_defined([maxhues,ll]); - huestep = 360 / maxhues; - huelist = [for (i=[0:1:ll-1]) posmod(i*huestep+i*360/stride,360)]; - hues = shuffle ? shuffle(huelist, seed=seed) : huelist; - for($idx=idx(list)) { - $item = list[$idx]; - HSV(h=hues[$idx]) children(); - } -} - - // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/shapes2d.scad b/shapes2d.scad index 68bf32b..e77d314 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -1429,4 +1429,75 @@ function reuleaux_polygon(N=3, r, d, anchor=CENTER, spin=0) = ) reorient(anchor,spin, two_d=true, path=path, extent=false, anchors=anchors, p=path); + +// Section: Rounding 2D shapes + +// Module: round2d() +// Usage: +// round2d(r) ... +// round2d(or) ... +// round2d(ir) ... +// round2d(or, ir) ... +// Description: +// Rounds arbitrary 2D objects. Giving `r` rounds all concave and convex corners. Giving just `ir` +// rounds just concave corners. Giving just `or` rounds convex corners. Giving both `ir` and `or` +// can let you round to different radii for concave and convex corners. The 2D object must not have +// any parts narrower than twice the `or` radius. Such parts will disappear. +// Arguments: +// r = Radius to round all concave and convex corners to. +// or = Radius to round only outside (convex) corners to. Use instead of `r`. +// ir = Radius to round only inside (concave) corners to. Use instead of `r`. +// Examples(2D): +// round2d(r=10) {square([40,100], center=true); square([100,40], center=true);} +// round2d(or=10) {square([40,100], center=true); square([100,40], center=true);} +// round2d(ir=10) {square([40,100], center=true); square([100,40], center=true);} +// round2d(or=16,ir=8) {square([40,100], center=true); square([100,40], center=true);} +module round2d(r, or, ir) +{ + or = get_radius(r1=or, r=r, dflt=0); + ir = get_radius(r1=ir, r=r, dflt=0); + offset(or) offset(-ir-or) offset(delta=ir,chamfer=true) children(); +} + + +// Module: shell2d() +// Usage: +// shell2d(thickness, [or], [ir], [fill], [round]) +// Description: +// Creates a hollow shell from 2D children, with optional rounding. +// Arguments: +// thickness = Thickness of the shell. Positive to expand outward, negative to shrink inward, or a two-element list to do both. +// or = Radius to round corners on the outside of the shell. If given a list of 2 radii, [CONVEX,CONCAVE], specifies the radii for convex and concave corners separately. Default: 0 (no outside rounding) +// ir = Radius to round corners on the inside of the shell. If given a list of 2 radii, [CONVEX,CONCAVE], specifies the radii for convex and concave corners separately. Default: 0 (no inside rounding) +// Examples(2D): +// shell2d(10) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(-10) {square([40,100], center=true); square([100,40], center=true);} +// shell2d([-10,10]) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,or=10) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,ir=10) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,or=[10,0]) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,or=[0,10]) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,ir=[10,0]) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(10,ir=[0,10]) {square([40,100], center=true); square([100,40], center=true);} +// shell2d(8,or=[16,8],ir=[16,8]) {square([40,100], center=true); square([100,40], center=true);} +module shell2d(thickness, or=0, ir=0) +{ + thickness = is_num(thickness)? ( + thickness<0? [thickness,0] : [0,thickness] + ) : (thickness[0]>thickness[1])? ( + [thickness[1],thickness[0]] + ) : thickness; + orad = is_finite(or)? [or,or] : or; + irad = is_finite(ir)? [ir,ir] : ir; + difference() { + round2d(or=orad[0],ir=orad[1]) + offset(delta=thickness[1]) + children(); + round2d(or=irad[1],ir=irad[0]) + offset(delta=thickness[0]) + children(); + } +} + + // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/std.scad b/std.scad index e085e42..ccfa7b6 100644 --- a/std.scad +++ b/std.scad @@ -13,6 +13,7 @@ include include include include +include include include include