discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

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

BW
Bernd Wechner
Mon, Apr 27, 2020 10:43 AM

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 > b_n and b_n >an and
a_n >a_n-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.

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 > b_n and b_n >an and a_n >a_n-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.
TP
Torsten Paul
Mon, Apr 27, 2020 11:22 AM

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]]

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]]