Translating points (number pairs) from one representation to another ...

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Translating points (number pairs) from one representation to another ...

OpenSCAD mailing list-2

I just ran into the problem that I think most everyone learning scad run into at some point.

I though to variables as assignable, and nothing worked right until I read:

��� https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

and now I know why. But given that's a totally new paradigm for me (and many) but one I'd like to master all the same, I have a problem I can solve with an imperative language and wonder how it's done with a functional language like scad.

Given two sets:

��� [0,L], [[a1,b1], [a2,b2], ...., [an,bn]]

where the second set is of unknown length, I would like translate this to:

��� [ [0,a1], [b1,a2], [b2, a3],....[bn,L] ]

Having trouble getting my head around for constructs and how they might be used to do that if indeed that's the approach, or how to write a function that works on arbitrary length of the second set and output.

If it's any use, which I doubt, in my use case, L > bn and bn>an and an>an-1.

I have a gut feel of course that such translations must be possible, in fact possibly trivial once the mindset is right, but I'm lacking the insight.

Kind regards,

Bernd.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Translating points (number pairs) from one representation to another ...

tp3
Maybe this way using list comprehension to generate a new list
while iterating over indices 0 to length of the list (which
means one iteration more than elements of the list).

function transform(a, list) = [
    for (i = [0:len(list)])
    i == 0 ? [a[0], list[i][0]]
           : i < len(list) ? [list[i-1][1], list[i][0]]
                           : [list[i-1][1], a[1]]
];

echo(transform([1, 9], [[10, 11], [20, 21], [30, 31], [40, 41]]));
// ECHO: [[1, 10], [11, 20], [21, 30], [31, 40], [41, 9]]


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten