Eighth example, lookup :
It looks ok, but my lookup function is maybe buggy, I needed to stretch return value (x2) to have same lengths as V1, and to add a center to each cylinders to make it look the same.
// title : Lookup
// author : OpenSCAD.org, adapted by Rene K. Mueller
// description: testing lookup() function
// file : lookup.jscad
// from http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions
const jscad = require('@jscad/modeling')
const { primitives, transforms, colors, maths } = jscad
const { cylinder } = primitives
const { translate } = transforms
const { colorize, hslToRgb } = colors
const { vec2 } = maths
const { lerp } = vec2
function getCylinderH (p) {
return lookup(p,
[ [ -200, 5 ], [ -50, 20 ], [ -20, 18 ], [ +80, 25 ], [ +150, 2 ] ]);
}
function main () {
var w = [];
for (var i = -100; i <= 100; i += 5) {
w.push(colorize(hslToRgb([((i + 100) / 200) * 0.3 + 0.6, 1, 0.5]),
translate([i, 0, -30],
cylinder({radius: 4,
height: getCylinderH(i) * 3 *2, segments: 16
, center : [i,0,getCylinderH(i) * 3]
}))));
}
return w;
}
function lookup(ix, L){ // L = [ [ i0, v0], ..., [iN, vN] ]
if (ix <= L[0][0])
return L[0][0];
var i = L.length-1;
if (ix >= L[i][0])
return L[i][0];
i = L.findIndex( x => x[0] >= ix);
v1 = L[i-1][1];
v2 = L[i][1];
if (ix === L[i-1][0])
return v1;
if (ix === L[i][0])
return v2;
var i1 = L[i-1][0], i2 = L[i][0];
var r = v1 + (v2-v1) / (i2-i1) * (ix-i1);
return r;
}
module.exports = { main }