# Easy way to accumulate a sum?

4 messages
Open this post in threaded view
|

## Easy way to accumulate a sum?

 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);           }          }
Open this post in threaded view
|

## Re: Easy way to accumulate a sum?

 2017-03-20 13:19 GMT-03:00 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);     }    } -- 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
 Separating the two tasks and using norm(),  it becomes more readable:`function add(v, i=0, r=0) = i