Hi there,
I think I got the use of modules, I almost manage to get what I'm looking for. Now that I'd like to work more on calculating new values and changing values inside vectors (producing new vectors) before using modules to "draw" the results, I'm facing strange issues... Using the same kind of code refactored so that it can fit within a function, I don't get the same results. It must be obvious, I'm doing something super wrong, but for the time being, I just can't spot it. Here is an over simplified version of the issues I'm facing. feet = [[0,0,0], [77,0,0], [14,45.50,0], [63,45.5,0]]; trapezium(feet); echo(trapezoid(feet)); module trapezium(points) { // the base 1 is point 0 to point 1 // the base 2 is point 2 to point 3 bigBase = points[1][0] - points[0][0]; littleBase = points[3][0] - points[2][0]; height = points[3][1] - points[0][1]; area = (bigBase*littleBase*height)/2; echo("Big base is : ", bigBase, ", small base is : ", littleBase, ", height is : ", height, ", area is : ", area); } function trapezoid(points) = (points[1][0] - points[0][0]*points[3][0] - points[2][0]*points[3][1] - points[0][1])/2; That snippet of code gives me ECHO: "Big base is : ", 77, ", small base is : ", 49, ", height is : ", 45.5, ", area is : ", 85835.8 ECHO: -280 What did I miss? Thanks a lot, -- Bruno Verachten _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
I suggest that your problem is you wrote the function in a cryptic way,
removing all the intermediate steps that make it debuggable. Instead, do it like this: function trapezoid(points) = let( bigBase = points[1][0] - points[0][0], littleBase = points[3][0] - points[2][0], height = points[3][1] - points[0][1], area = (bigBase*littleBase*height)/2 ) area; I don't know what's wrong with your cryptic version, but the above version matches the behavior of the module. gounthar wrote > Hi there, > > I think I got the use of modules, I almost manage to get what I'm looking > for. > Now that I'd like to work more on calculating new values and changing > values inside vectors (producing new vectors) before using modules to > "draw" the results, I'm facing strange issues... > Using the same kind of code refactored so that it can fit within a > function, I don't get the same results. > It must be obvious, I'm doing something super wrong, but for the time > being, I just can't spot it. > Here is an over simplified version of the issues I'm facing. > feet = [[0,0,0], [77,0,0], [14,45.50,0], [63,45.5,0]]; > trapezium(feet); > echo(trapezoid(feet)); > > module trapezium(points) { > // the base 1 is point 0 to point 1 > // the base 2 is point 2 to point 3 > bigBase = points[1][0] - points[0][0]; > littleBase = points[3][0] - points[2][0]; > height = points[3][1] - points[0][1]; > area = (bigBase*littleBase*height)/2; > echo("Big base is : ", bigBase, ", small base is : ", littleBase, > ", height is : ", height, ", area is : ", area); > } > > function trapezoid(points) = (points[1][0] - points[0][0]*points[3][0] > - points[2][0]*points[3][1] - points[0][1])/2; > > That snippet of code gives me > ECHO: "Big base is : ", 77, ", small base is : ", 49, ", height is : > ", 45.5, ", area is : ", 85835.8 > ECHO: -280 > > What did I miss? > > Thanks a lot, > -- > Bruno Verachten > > _______________________________________________ > OpenSCAD mailing list > Discuss@.openscad > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Sent from: http://forum.openscad.org/ _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
In reply to this post by gounthar
You need brackets around the subtractions so they are done before the multiplication. You can make it more readable by using .x for [0] and .y for [1]. You can also use let() to assign partial results to variables like your module. On Wed, 30 Dec 2020 at 14:26, Verachten Bruno <[hidden email]> wrote: Hi there, _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
Stupid me!
function trapezoid(points) = ([points[1][0] - points[0][0]]*[points[3][0] - points[2][0]]*[points[3][1] - points[0][1]])/2; does work the same. Thanks a lot all for your (much needed) help! On Wed, Dec 30, 2020 at 3:38 PM nop head <[hidden email]> wrote: > > You need brackets around the subtractions so they are done before the multiplication. > > You can make it more readable by using .x for [0] and .y for [1]. > > You can also use let() to assign partial results to variables like your module. > > On Wed, 30 Dec 2020 at 14:26, Verachten Bruno <[hidden email]> wrote: >> >> Hi there, >> >> I think I got the use of modules, I almost manage to get what I'm looking for. >> Now that I'd like to work more on calculating new values and changing >> values inside vectors (producing new vectors) before using modules to >> "draw" the results, I'm facing strange issues... >> Using the same kind of code refactored so that it can fit within a >> function, I don't get the same results. >> It must be obvious, I'm doing something super wrong, but for the time >> being, I just can't spot it. >> Here is an over simplified version of the issues I'm facing. >> feet = [[0,0,0], [77,0,0], [14,45.50,0], [63,45.5,0]]; >> trapezium(feet); >> echo(trapezoid(feet)); >> >> module trapezium(points) { >> // the base 1 is point 0 to point 1 >> // the base 2 is point 2 to point 3 >> bigBase = points[1][0] - points[0][0]; >> littleBase = points[3][0] - points[2][0]; >> height = points[3][1] - points[0][1]; >> area = (bigBase*littleBase*height)/2; >> echo("Big base is : ", bigBase, ", small base is : ", littleBase, >> ", height is : ", height, ", area is : ", area); >> } >> >> function trapezoid(points) = (points[1][0] - points[0][0]*points[3][0] >> - points[2][0]*points[3][1] - points[0][1])/2; >> >> That snippet of code gives me >> ECHO: "Big base is : ", 77, ", small base is : ", 49, ", height is : >> ", 45.5, ", area is : ", 85835.8 >> ECHO: -280 >> >> What did I miss? >> >> Thanks a lot, >> -- >> Bruno Verachten >> >> _______________________________________________ >> 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 -- Bruno Verachten _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
It should really be parenthesis (round brackets). I think you fluked the right answer with square brackets because you made the differences into one element vectors and then multiplied those, which gives a scalar product. On Wed, 30 Dec 2020 at 14:44, Verachten Bruno <[hidden email]> wrote: Stupid me! _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
Free forum by Nabble | Edit this page |