Quantcast

Easy way to accumulate a sum?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Easy way to accumulate a sum?

jazzjohn
From the manual:
"...the concept of x = x + 1  is not valid, get to understand this concept and you will understand the beauty of OpenSCAD."

Coming from a C background, I have a ways to go before I understand this particular "beauty" !

Does anyone know of a generic summing function? My data has about 50 points so it not very large.




pts=[ [0,0], [100,100], [200,0], [300,100]]; //small test data example
   
Length();  
   
module Length() //computes total length of a 2D array
   {
   Total=0;    
   for (i=[0: len(pts) - 2])    
    {
     dx = pts[i+1][0]-pts[i][0];
     dy = pts[i+1][1]-pts[i][1];  
     D=sqrt(dx*dx+dy*dy);  
     Total = Total + D;  //I wish I could do this!  
     echo(Total);      
    }
     
   }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Easy way to accumulate a sum?

Ronaldo

2017-03-20 13:19 GMT-03:00 jazzjohn <[hidden email]>:
From the manual:
"...the concept of x = x + 1  is not valid, get to understand this concept
and you will understand the beauty of OpenSCAD."

Coming from a C background, I have a ways to go before I understand this
particular "beauty" !

Does anyone know of a generic summing function? My data has about 50 points
so it not very large.




pts=[ [0,0], [100,100], [200,0], [300,100]]; //small test data example

Length();

module Length() //computes total length of a 2D array
   {
   Total=0;
   for (i=[0: len(pts) - 2])
    {
     dx = pts[i+1][0]-pts[i][0];
     dy = pts[i+1][1]-pts[i][1];
     D=sqrt(dx*dx+dy*dy);
     Total = Total + D;  //I wish I could do this!
     echo(Total);
    }

   }



--
View this message in context: http://forum.openscad.org/Easy-way-to-accumulate-a-sum-tp20963.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

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


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

Re: Easy way to accumulate a sum?

jazzjohn
Thank you!

This seems to work, (though not very pretty):

function add(v, i=0,r=0) = i<len(v)-1 ?
add(v, i+1,r    +   sqrt( (v[i+1][0]-v[i][0]) * (v[i+1][0]-v[i][0])  
+(v[i+1][1]-v[i][1]) * (v[i+1][1]-v[i][1]) )
) : r;

pts = [ [0,0],[100,100],[200,0],[300,100] ];

out = add(pts);
echo(out);

ECHO: 424.264
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Easy way to accumulate a sum?

Ronaldo
Separating the two tasks and using norm(),  it becomes more readable:

function add(v, i=0, r=0) = i<len(v) ? add(v, i+1, r+v[i]) : r;
function lengths(v) = [for(i=[0:len(v)-2]) norm(v[i+1]-v[i]) ];
out = add(lengths(v));

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