-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Summing from a lists - question #4308
Comments
The mailing list is a better place to ask a "how to" question. The answer is recursion.
Similar techniques can be used, e.g., to construct a new array with the offsets:
|
Thank you on both answers. I was looking for a better place to ask the question and didn't see the alternative. I was looking for a way to get the translate()'s to "stack up" and recursion didn't occur to me. Very elegant solution. Thanks again. |
Another technique is to use the "C-style
I don't know why "C-style One might think that this violates the "variables are immutable" rule. I would say that it does not, in the same way that any other |
Wow, I didn't know you could do the x=.... as a FOR parameter and have it calculate different x's. I've definitely learned a lot from this question. Thanks again. |
One might think that this violates the "variables are immutable" rule. I would say that it does not, in the same way that any other "for" loop does not: it creates new instances of the variable for each iteration.
I have on my list to do something in the wiki to explain that there is no iteration of geometry FOR loops.
It is a branching of the CSG Tree, each instance in parallel,
for (i=[-1:+1]) translate([i*10,0,0]) cube(2);
group() {
multmatrix([[1, 0, 0, -10], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
cube(size = [2, 2, 2], center = false);
}
multmatrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
cube(size = [2, 2, 2], center = false);
}
multmatrix([[1, 0, 0, 10], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) {
cube(size = [2, 2, 2], center = false);
}
}
I haven't checked, but I expect with a large loop the multithreaded version would not process them in order.
…_____
From: jordanbrown0 ***@***.***
Sent: Wed, 27 Jul 2022 03:21
To: openscad/openscad
Cc: Subscribed
Subject: Re: [openscad/openscad] Summing from a lists - question (Issue #4308)
Another technique is to use the "C-style for" available in list comprehensions.
module row_of_cylinders_3(h, diameters, spacing) {
offsets = [
for(
i = 0, x=0;
i < len(diameters);
x = x + diameters[i] + spacing, i = i + 1
) x
];
for (i=[0:len(diameters)-1]) {
translate([offsets[i],0,0]) cylinder(h=h, d=diameters[i]);
}
}
translate([0,20,0]) row_of_cylinders_3(h=h, diameters=Diameters, spacing=spacing);
I don't know why "C-style for" is not available for top-level for loops, but it doesn't seem to be.
One might think that this violates the "variables are immutable" rule. I would say that it does not, in the same way that any other "for" loop does not: it creates new instances of the variable for each iteration. The difference is that with C-style for the new instance can be based on the previous instance.
—
Reply to this email directly, view <#4308 (comment)> it on GitHub, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAPBVYJPOTDLVZTMTAMD4L3VWANAFANCNFSM54WKUZVA> .
You are receiving this because you are subscribed to this thread. <https://github.com/notifications/beacon/AAPBVYIGVO2NXP3FO6XJHJLVWANAFA5CNFSM54WKUZVKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOI5C6LRI.gif> Message ID: ***@***.***>
--
This email has been checked for viruses by AVG.
https://www.avg.com
|
There has to be a way to do this, but I'm not seeing how:
I have a list of 10 cylinder diameters and a distance between adjacent cylinders:
Diameters = [1, 3, 2, 7, 3, 4, 9, 8, 4, 5];
Spacing = 5;
I'd like to be able to step through list and create each cylinder. So to find the center of cylinder i, I would need to add up the diameters of all previous cylinders (plus the appropriate spacing, Spacing*(i-1), and then use that for a translate();
However, with immutable variables I can't add things based on a parameter i (for example if I wanted to loop thru the 10 items in the list).
There has to be a way of doing this, but I'm not seeing it, other than a bunch of "if" statements based on a value of i:
if (i==0) { translate([0, ...]) cylinder(d=Diameter[i], h=100); }
if (i==1) { translate([Spacingi+Diameter[0], ...]) cylinder(d=Diameter[i], h=100); }
if (i==2) { translate([Spacingi+Diameter[0]+Diameter[1], ...]) cylinder(d=Diameter[i], h=100); }
if (i==3) { translate([Spacing*i+Diameter[0]+Diameter[1]+Diameter[2], ...]) cylinder(d=Diameter[i], h=100); }
... etc....
Any ideas?
Thanks!
The text was updated successfully, but these errors were encountered: