Is it possible to use results of loops in openScads quicksort() ?

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

Is it possible to use results of loops in openScads quicksort() ?

Bo Nystedt

I am new to this forum, and perhaps this is not where to ask the question?

I thought I posted the question to the forum yesterday, but I have received no confirmation by email that I am accepted in this forum.

So I repeat my question and hope for an answer - at least whether I am accepted or not.

Best regards

Bo Nystedt

Arrays openScad : I need to use the results of nested loops in quicksort() function but it seems impossible.

'arr' in second last line (in code below) will not contain more than one value at a time. In the first run of the loop arr gets the value 2.01, in second run the value is 1.6125, and so on. What I need is an array containing all four values in this way: arr=[[2.01],[1.6125],[3.775],[4.8]], because quicksort needs such an input. If is it possible to achieve that in openScad, how do I do it?


function quicksort(arr) = !(len(arr)>0) ? [] : let(
pivot   = arr[floor(len(arr)/2)],
lesser  = [ for (y = arr) if (y  < pivot) y ],
equal   = [ for (y = arr) if (y == pivot) y ],
greater = [ for (y = arr) if (y  > pivot) y ]
) concat(quicksort(lesser), equal, quicksort(greater));

x_0=262; y_0=140; x_1=17.5;y_1=305;x_2=500;y_2=255; x_3=575;y_3=385;

ads=[[x_0,y_0],[x_1,y_1],[x_2,y_2],[x_3,y_3]];
for(b=[0 : 3])
{
 for(c=[0:0])
    if(ads[b][0]>=0 && ads[b][1]>=0)
    {         
            asc_diff=((ads[b][0])+ads[b][1])/2*0.01;                    
            arr=asc_diff; 
            //quicksort(arr) ;          
            echo("27: arr", arr); 
      }
 }




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

Re: Is it possible to use results of loops in openScads quicksort() ?

Parkinbot
You can use  list comprehensions
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions>  to
compose lists:



--
Sent from: http://forum.openscad.org/

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