diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2010-01-13 17:03:32 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2010-01-13 17:03:32 (GMT) |
commit | 96002da9cdfce46c80f256652281328587aa41c3 (patch) | |
tree | da22452811d601b565474c4692e4062a35112c0f | |
parent | 7f944e1e25faa8d8c077ba78e4fd489b69270fb5 (diff) |
Clifford Wolf:
Added list() function
git-svn-id: http://svn.clifford.at/openscad/trunk@276 b57f626f-c46c-0410-a088-ec61d464b74c
-rw-r--r-- | examples/example019.scad | 14 | ||||
-rw-r--r-- | func.cc | 29 |
2 files changed, 43 insertions, 0 deletions
diff --git a/examples/example019.scad b/examples/example019.scad new file mode 100644 index 0000000..4ae9f70 --- /dev/null +++ b/examples/example019.scad @@ -0,0 +1,14 @@ + +function get_cylinder_h(p) = list(p, [ + [ -200, 5 ], + [ -50, 20 ], + [ -20, 18 ], + [ +80, 25 ], + [ +150, 2 ] + ]); + +for (i = [-100:5:+100]) { + // echo(i, get_cylinder_h(i)); + translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3); +} + @@ -193,6 +193,34 @@ Value builtin_str(const QVector<QString>&, const QVector<Value> &args) return Value(str); } +Value builtin_list(const QVector<QString>&, const QVector<Value> &args) +{ + double p, low_p, low_v, high_p, high_v; + if (args.size() < 2 || !args[0].getnum(p) || args[1].vec.size() < 2 || args[1].vec[0]->vec.size() < 2) + return Value(); + if (!args[1].vec[0]->getv2(low_p, low_v) || !args[1].vec[0]->getv2(high_p, high_v)) + return Value(); + for (int i = 1; i < args[1].vec.size(); i++) { + double this_p, this_v; + if (args[1].vec[i]->getv2(this_p, this_v)) { + if (this_p <= p && (this_p > low_p || low_p > p)) { + low_p = this_p; + low_v = this_v; + } + if (this_p >= p && (this_p < high_p || high_p < p)) { + high_p = this_p; + high_v = this_v; + } + } + } + if (p <= low_p) + return Value(low_v); + if (p >= high_p) + return Value(high_v); + double f = (p-low_p) / (high_p-low_p); + return Value(high_v * f + low_v * (1-f)); +} + void initialize_builtin_functions() { builtin_functions["min"] = new BuiltinFunction(&builtin_min); @@ -206,6 +234,7 @@ void initialize_builtin_functions() builtin_functions["atan2"] = new BuiltinFunction(&builtin_atan2); builtin_functions["pow"] = new BuiltinFunction(&builtin_pow); builtin_functions["str"] = new BuiltinFunction(&builtin_str); + builtin_functions["list"] = new BuiltinFunction(&builtin_list); initialize_builtin_dxf_dim(); } |