[OpenSCAD] Clarifying behaviors
Ronaldo
rcmpersiano at gmail.com
Sun Oct 2 18:04:03 EDT 2016
Valuable information, don bright. Thank you.
donbright wrote
> If you dig around the tests/scad you can find tests for vector / list
> operations. I am not sure if the complicated ones you are using are in
> there, but they might be.
In a first quick look, I found just simple tests with homogeneous uni and
bi-dimensional lists. But I found many interesting and surprising others. As
a lazy man, when I want to check something I generate random data, animate
the code and sit back staring at the console...
My use of those properties is neither exoctic nor complicated. I will give
you a simple example. Suppose you define a function performing the
calculations of a polynomial given its coefficients:
> function polynomial(coef, u) =
> sum_list([ for(i=[0:len(coef)-1]) coef[i]*pow(u,i) ]);
Although, the code has been written for a polynomial with real coefficients
it can be used for more general cases if you observe that the only
operations with parameter coef inside this function are additions and scalar
products by the power factors. In fact, we may use 2D or 3D points and
vectors as coefficients of the function and define a parametric curve in the
space without any change in its code:
> function curve(p,n) = [ for(i=[0:n]) polynomial(p,i/n)];
> p = [ [0,0], [1,0], [1,1] ];
> echo(curve(p,5));
> // ECHO: [[0, 0], [0.24, 0.04], [0.56, 0.16], [0.96, 0.36], [1.44, 0.64],
> [2, 1]]
We may go further and compute the mesh of parametric 3D surfaces from
curve() and polynomial() by:
> function surface(q, n, m) =
> let( c = curve(q, n) ) [ for(i=[0:m]) polynomial(c,i/m) ];
>
> q = [ [ [0,0,0], [1,0,0], [1,1,0] ],
> [ [0,0,1], [1,0,1], [1,1,1] ],
> [ [0,0,2], [1,0,2], [1,1,2] ] ];
>
> echo(surface(q,2,3));
> // ECHO: [ [[0, 0, 0], [1, 0, 0], [1, 1, 0]],
> // [[0, 0, 0.666667], [1.91667, 0, 0.666667], [1.91667,
> 1.91667, 0.666667]],
> // [[0, 0, 2], [3.5, 0, 2], [3.5, 3.5, 2]], [[0, 0, 4], [5.75,
> 0, 4], [5.75, 5.75, 4]]]
Note that the input argument coef of polynomial() is a list of points in
curve() and a matrix of points in surface(). And that the results of the
function polynomial() are homogeneous list of objects of the same type of
coef. The fundamental concept here is that the starting function is linear
in respect to the argument coef.
That code conciseness is impossible with typed programming languages.
--
View this message in context: http://forum.openscad.org/Clarifying-behaviors-tp18492p18509.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
More information about the Discuss
mailing list