discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Easy way to accumulate a sum?

J
jazzjohn
Mon, Mar 20, 2017 4:19 PM

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(dxdx+dydy);
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.

>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.
RP
Ronaldo Persiano
Mon, Mar 20, 2017 4:27 PM

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(dxdx+dydy);
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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

See: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Add_all_values_in_a_list 2017-03-20 13:19 GMT-03:00 jazzjohn <jazzjohn@gmail.com>: > 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
J
jazzjohn
Mon, Mar 20, 2017 5:48 PM

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

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

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 -- View this message in context: http://forum.openscad.org/Easy-way-to-accumulate-a-sum-tp20963p20966.html Sent from the OpenSCAD mailing list archive at Nabble.com.
RP
Ronaldo Persiano
Mon, Mar 20, 2017 6:07 PM

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));

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));