discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Beginner needs help

D
droftarts
Tue, Jan 24, 2017 4:15 PM

For the mod, I asked the same questions in the thread the function comes
from, which nophead replied to:
http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16553.html
A little later on I did a small example to help me understand it!
http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16623.html

Ian

--
View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20214.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

For the mod, I asked the same questions in the thread the function comes from, which nophead replied to: http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16553.html A little later on I did a small example to help me understand it! http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16623.html Ian -- View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20214.html Sent from the OpenSCAD mailing list archive at Nabble.com.
S
stressless
Wed, Jan 25, 2017 7:08 AM

Thanks droftarts. Interesting!

I didn't see this being documented anywhere. It would be worth putting it in
the manual.

Dan

droftarts wrote

For the mod, I asked the same questions in the thread the function comes
from, which nophead replied to:
http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16553.html
A little later on I did a small example to help me understand it!
http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16623.html

Ian

--
View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20217.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Thanks droftarts. Interesting! I didn't see this being documented anywhere. It would be worth putting it in the manual. Dan droftarts wrote > For the mod, I asked the same questions in the thread the function comes > from, which nophead replied to: > http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16553.html > A little later on I did a small example to help me understand it! > http://forum.openscad.org/Script-to-replicate-hull-and-minkoswki-for-CSG-export-import-into-FreeCAD-tp16537p16623.html > > Ian -- View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20217.html Sent from the OpenSCAD mailing list archive at Nabble.com.
D
droftarts
Wed, Jan 25, 2017 11:43 AM

It could probably be added to the page of tips and tricks here:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks

Ian

--
View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20219.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

It could probably be added to the page of tips and tricks here: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks Ian -- View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20219.html Sent from the OpenSCAD mailing list archive at Nabble.com.
S
stressless
Wed, Jan 25, 2017 6:24 PM

As I was perplexed about the use of MOD (%) in a loop using a list, I did a
little testing and research of my own.Apparently, the syntax (points[(i) %
length]) divides the value of Point(i) by the array's length and returns the
remainder (oh-la-la).Here is the way I was recommended to go :points =
[17,1,2,3,4];length = len (points);for (i=[0:length-1]) {    remainder =
(points[(i) % length]);    echo (points[(i) % length],points[(i+1) %
length]);    echo ("remainder : ", remainder);}and its console output
:ECHO: 17, 1ECHO: "remainder : ", 17ECHO: 1, 2ECHO: "remainder : ", 1ECHO:
2, 3ECHO: "remainder : ", 2ECHO: 3, 4ECHO: "remainder : ", 3ECHO: 4,
17ECHO: "remainder : ", 4In the one before last ECHO, we notice the 17
which is Pos 0 of the list.When we take both '% length' away as so :points =
[17,1,2,3,4];length = len (points);for (i=[0:length-1]) {    remainder =
(points[(i) % length]);    echo (points[(i)],points[(i+1)]);    echo
("remainder : ", remainder);}The console output is as follows :ECHO: 17,
1ECHO: "remainder : ", 17ECHO: 1, 2ECHO: "remainder : ", 1ECHO: 2, 3ECHO:
"remainder : ", 2ECHO: 3, 4ECHO: "remainder : ", 3ECHO: 4, undefECHO:
"remainder : ", 4In this case, the one before last ECHO says 'undef'Looking
at the tips & Tricks page you mentioned, I noticed this post
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef
which essentially says this : "Most illegal operations in OpenSCAD return
undef. Some return nan. However the program keeps running and undef values
may cause unpredictable future behaviour if no precaution is taken".In
short, not using MOD (%) in a loop using a list may cause problems. That's
that !However, the reason why the use of MOD resets the loop to the first
item of the list remains a mystery to me. But at least I've learned
something, thanks to all the contributors.Cheers, Dan

--
View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20231.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

As I was perplexed about the use of MOD (%) in a loop using a list, I did a little testing and research of my own.Apparently, the syntax (points[(i) % length]) divides the value of Point(i) by the array's length and returns the remainder (oh-la-la).Here is the way I was recommended to go :points = [17,1,2,3,4];length = len (points);for (i=[0:length-1]) { remainder = (points[(i) % length]); echo (points[(i) % length],points[(i+1) % length]); echo ("remainder : ", remainder);}and its console output :ECHO: 17, 1ECHO: "remainder : ", 17ECHO: 1, 2ECHO: "remainder : ", 1ECHO: 2, 3ECHO: "remainder : ", 2ECHO: 3, 4ECHO: "remainder : ", 3ECHO: 4, *17*ECHO: "remainder : ", 4In the one before last ECHO, we notice the 17 which is Pos 0 of the list.When we take both '% length' away as so :points = [17,1,2,3,4];length = len (points);for (i=[0:length-1]) { remainder = (points[(i) % length]); echo (points[(i)],points[(i+1)]); echo ("remainder : ", remainder);}The console output is as follows :ECHO: 17, 1ECHO: "remainder : ", 17ECHO: 1, 2ECHO: "remainder : ", 1ECHO: 2, 3ECHO: "remainder : ", 2ECHO: 3, 4ECHO: "remainder : ", 3ECHO: 4, *undef*ECHO: "remainder : ", 4In this case, the one before last ECHO says 'undef'Looking at the tips & Tricks page you mentioned, I noticed this post https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef which essentially says this : "Most illegal operations in OpenSCAD return undef. Some return nan. However the program keeps running and undef values may cause unpredictable future behaviour if no precaution is taken".In short, not using MOD (%) in a loop using a list may cause problems. That's that !However, the reason why the use of MOD resets the loop to the first item of the list remains a mystery to me. But at least I've learned something, thanks to all the contributors.Cheers, Dan -- View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20231.html Sent from the OpenSCAD mailing list archive at Nabble.com.
RP
Ronaldo Persiano
Wed, Jan 25, 2017 7:29 PM

If your intention is to use mod (%) to go through a circular list of
´points, you should apply it to the point index and not to the point itself.
As an example, suppose you want to draw the edges of a polygon whose points
are in a list pt.

// makes a set of "cylinders" representing the edges of a polygon
module draw_polygon(pt) {
for(i=[0:len(pt)-1])  // for each point
hull() { // makes a "cylinder" from point pt[i] to the next
translate(pt[i]) sphere();
translate(pt[i+1]) sphere();
}
}
draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]);

This doesn't work as intended because when i==len(pt)-1 the next point
would be pt[len(pt)] which does not exist. It should be pt[0].

You may correct this code including a test:

module draw_polygon(pt) {
for(i=[0:len(pt)-1])  // for each point
hull() { // makes a "cylinder" from point pt[i] to the next
translate(pt[i]) sphere();
if(i==len(pt)-1)
translate(pt[0]) sphere();
translate(pt[i+1]) sphere();
}
}

which is cumbersome. Instead, use mod operator:

module draw_polygon(pt) {
for(i=[0:len(pt)-1])  // for each point
hull() { // makes a "cylinder" from point pt[i] to the next
translate(pt[i]) sphere();
translate(pt[(i+1)%len(pt)]) sphere();
}
}

Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as
intended.

2017-01-25 16:24 GMT-02:00 stressless wintoweb@gmail.com:

As I was perplexed about the use of MOD (%) in a loop using a list, I did
a little testing and research of my own. Apparently, the syntax (points[(i)
% length]) divides the value of Point(i) by the array's length and returns
the remainder (oh-la-la). Here is the way I was recommended to go : points
= [17,1,2,3,4]; length = len (points); for (i=[0:length-1]) { remainder =
(points[(i) % length]); echo (points[(i) % length],points[(i+1) % length]);
echo ("remainder : ", remainder); } and its console output : ECHO: 17, 1
ECHO: "remainder : ", 17 ECHO: 1, 2 ECHO: "remainder : ", 1 ECHO: 2, 3
ECHO: "remainder : ", 2 ECHO: 3, 4 ECHO: "remainder : ", 3 ECHO: 4,

17 ECHO: "remainder : ", 4 In the one before last ECHO, we notice the 17
which is Pos 0 of the list. When we take both '% length' away as so :
points = [17,1,2,3,4]; length = len (points); for (i=[0:length-1]) {
remainder = (points[(i) % length]); echo (points[(i)],points[(i+1)]); echo
("remainder : ", remainder); } The console output is as follows : ECHO: 17,
1 ECHO: "remainder : ", 17 ECHO: 1, 2 ECHO: "remainder : ", 1 ECHO: 2, 3
ECHO: "remainder : ", 2 ECHO: 3, 4 ECHO: "remainder : ", 3 ECHO: 4, undef
ECHO: "remainder : ", 4 In this case, the one before last ECHO says 'undef'
Looking at the tips & Tricks page you mentioned, I noticed this post
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef
which essentially says this : "Most illegal operations in OpenSCAD return
undef. Some return nan. However the program keeps running and undef values
may cause unpredictable future behaviour if no precaution is taken". In
short, not using MOD (%) in a loop using a list may cause problems. That's
that ! However, the reason why the use of MOD resets the loop to the first
item of the list remains a mystery to me. But at least I've learned
something, thanks to all the contributors. Cheers, Dan
------------------------------ View this message in context: Re: Beginner
needs help
http://forum.openscad.org/Beginner-needs-help-tp20159p20231.html Sent
from the OpenSCAD mailing list archive http://forum.openscad.org/ at
Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

If your intention is to use mod (%) to go through a circular list of ´points, you should apply it to the point index and not to the point itself. As an example, suppose you want to draw the edges of a polygon whose points are in a list pt. // makes a set of "cylinders" representing the edges of a polygon module draw_polygon(pt) { for(i=[0:len(pt)-1]) // for each point hull() { // makes a "cylinder" from point pt[i] to the next translate(pt[i]) sphere(); translate(pt[i+1]) sphere(); } } draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]); This doesn't work as intended because when i==len(pt)-1 the next point would be pt[len(pt)] which does not exist. It should be pt[0]. You may correct this code including a test: module draw_polygon(pt) { for(i=[0:len(pt)-1]) // for each point hull() { // makes a "cylinder" from point pt[i] to the next translate(pt[i]) sphere(); if(i==len(pt)-1) translate(pt[0]) sphere(); translate(pt[i+1]) sphere(); } } which is cumbersome. Instead, use mod operator: module draw_polygon(pt) { for(i=[0:len(pt)-1]) // for each point hull() { // makes a "cylinder" from point pt[i] to the next translate(pt[i]) sphere(); translate(pt[(i+1)%len(pt)]) sphere(); } } Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as intended. 2017-01-25 16:24 GMT-02:00 stressless <wintoweb@gmail.com>: > As I was perplexed about the use of MOD (%) in a loop using a list, I did > a little testing and research of my own. Apparently, the syntax (points[(i) > % length]) divides the value of Point(i) by the array's length and returns > the remainder (oh-la-la). Here is the way I was recommended to go : points > = [17,1,2,3,4]; length = len (points); for (i=[0:length-1]) { remainder = > (points[(i) % length]); echo (points[(i) % length],points[(i+1) % length]); > echo ("remainder : ", remainder); } and its console output : ECHO: 17, 1 > ECHO: "remainder : ", 17 ECHO: 1, 2 ECHO: "remainder : ", 1 ECHO: 2, 3 > ECHO: "remainder : ", 2 ECHO: 3, 4 ECHO: "remainder : ", 3 ECHO: 4, > > > *17 ECHO: "remainder : ", 4 In the one before last ECHO, we notice the 17 > which is Pos 0 of the list. When we take both '% length' away as so : > points = [17,1,2,3,4]; length = len (points); for (i=[0:length-1]) { > remainder = (points[(i) % length]); echo (points[(i)],points[(i+1)]); echo > ("remainder : ", remainder); } The console output is as follows : ECHO: 17, > 1 ECHO: "remainder : ", 17 ECHO: 1, 2 ECHO: "remainder : ", 1 ECHO: 2, 3 > ECHO: "remainder : ", 2 ECHO: 3, 4 ECHO: "remainder : ", 3 ECHO: 4, undef > ECHO: "remainder : ", 4 In this case, the one before last ECHO says 'undef' > Looking at the tips & Tricks page you mentioned, I noticed this post > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef > <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Caring_about_undef> > which essentially says this : "Most illegal operations in OpenSCAD return > undef. Some return nan. However the program keeps running and undef values > may cause unpredictable future behaviour if no precaution is taken". In > short, not using MOD (%) in a loop using a list may cause problems. That's > that ! However, the reason why the use of MOD resets the loop to the first > item of the list remains a mystery to me. But at least I've learned > something, thanks to all the contributors. Cheers, Dan > ------------------------------ View this message in context: Re: Beginner > needs help > <http://forum.openscad.org/Beginner-needs-help-tp20159p20231.html> Sent > from the OpenSCAD mailing list archive <http://forum.openscad.org/> at > Nabble.com.* > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
S
stressless
Thu, Jan 26, 2017 3:26 PM

Ronaldo,

Thanks for this. The use of MOD in a circular list of points make sense to
me now.
With your example, I also learned a creative way to use hull().
Great !

(and sorry for my messy original message. It went inadvertently in HTML
format. Now corrected)

Dan

Ronaldo wrote

draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]);

module draw_polygon(pt) {
for(i=[0:len(pt)-1])  // for each point
hull() { // makes a "cylinder" from point pt[i] to the next
translate(pt[i]) sphere();
translate(pt[(i+1)%len(pt)]) sphere();
}
}

// Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as
intended.

--
View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20242.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Ronaldo, Thanks for this. The use of MOD in a circular list of points make sense to me now. With your example, I also learned a creative way to use hull(). Great ! (and sorry for my messy original message. It went inadvertently in HTML format. Now corrected) Dan Ronaldo wrote > draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]); > > module draw_polygon(pt) { > for(i=[0:len(pt)-1]) // for each point > hull() { // makes a "cylinder" from point pt[i] to the next > translate(pt[i]) sphere(); > translate(pt[(i+1)%len(pt)]) sphere(); > } > } > > // Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as > intended. -- View this message in context: http://forum.openscad.org/Beginner-needs-help-tp20159p20242.html Sent from the OpenSCAD mailing list archive at Nabble.com.
JD
Jerry Davis
Sun, Jan 29, 2017 1:46 PM

stressless,

Sorry about the late post, I was camping this past week.

You can find the information about how to use the polygon builder tool, I
talked about, in the "Fusion360 and OpenSCAD" thread.
http://openscad.rocklinux.narkive.com/kayll2Vq/fusion360-and-openscad#post2

Jerry

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Openscad developer

The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

On Thu, Jan 26, 2017 at 8:26 AM, stressless wintoweb@gmail.com wrote:

Ronaldo,

Thanks for this. The use of MOD in a circular list of points make sense to
me now.
With your example, I also learned a creative way to use hull().
Great !

(and sorry for my messy original message. It went inadvertently in HTML
format. Now corrected)

Dan

Ronaldo wrote

draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]);

module draw_polygon(pt) {
for(i=[0:len(pt)-1])  // for each point
hull() { // makes a "cylinder" from point pt[i] to the next
translate(pt[i]) sphere();
translate(pt[(i+1)%len(pt)]) sphere();
}
}

// Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as
intended.

--
View this message in context: http://forum.openscad.org/
Beginner-needs-help-tp20159p20242.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

stressless, Sorry about the late post, I was camping this past week. You can find the information about how to use the polygon builder tool, I talked about, in the "Fusion360 and OpenSCAD" thread. http://openscad.rocklinux.narkive.com/kayll2Vq/fusion360-and-openscad#post2 Jerry -- Extra Ham Operator: K7AZJ Registered Linux User: 275424 Raspberry Pi and Openscad developer *The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".*- Isaac. Asimov On Thu, Jan 26, 2017 at 8:26 AM, stressless <wintoweb@gmail.com> wrote: > Ronaldo, > > Thanks for this. The use of MOD in a circular list of points make sense to > me now. > With your example, I also learned a creative way to use hull(). > Great ! > > (and sorry for my messy original message. It went inadvertently in HTML > format. Now corrected) > > Dan > > > Ronaldo wrote > > draw_polygon(pt=[ [0,0,0], [20,0,0], [0,20,0] ]); > > > > module draw_polygon(pt) { > > for(i=[0:len(pt)-1]) // for each point > > hull() { // makes a "cylinder" from point pt[i] to the next > > translate(pt[i]) sphere(); > > translate(pt[(i+1)%len(pt)]) sphere(); > > } > > } > > > > // Now, when i==len(pt-1) then i+1==len(pt) and so (i+1)%len(pt)==0 as > > intended. > > > > > > -- > View this message in context: http://forum.openscad.org/ > Beginner-needs-help-tp20159p20242.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 >