Dear all,
I am new to openScad. I want to generate random cubes on top of circle. The cubes should cover only the circle domain and not outside of it. I have written a script which takes x and y dimensions of cartesian coordinate and assigns the cube as a square. How to convert that to circular domain and assign only within the circular domain.? Below is the script. translate([0, 0, -0.5]) circle(14, $fn = 50); for (x = rands(-14, 14, 50), y = rands(-14, 14, 50)) translate([x, y, 0]) cube([0.5, 0.5, 0.5]); I have attached the circle and obtained cube image for reference. <http://forum.openscad.org/file/t3044/circle.png> Image: Cubes on circle <http://forum.openscad.org/file/t3044/circle_cube.png> I do not want the cubes to go out of the circular domain. I tired working with "pi" value and using cosine and sine, but could not been able to do correctly. Any leads will be helpful. Thanks and Regards, Sunag R A. -- Sent from: http://forum.openscad.org/ _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
You can place a cube inside a sphere (centered at the origin) by first
translating the cube on the X axis out to the radius of the circle, and then generate random A and B values between 0-356, and rotating the cube on 2 axies. You'll have to do some minor tweaking to get the center right. Hint: Use constants(variables) for all your numbers. Code left as an exercise for the user. ;-) Thus rasunag27 hast written on Mon, Jan 04, 2021 at 08:59:20PM -0700, and, according to prophecy, it shall come to pass that: > Dear all, > > I am new to openScad. I want to generate random cubes on top of circle. The > cubes should cover only the circle domain and not outside of it. > > I have written a script which takes x and y dimensions of cartesian > coordinate and assigns the cube as a square. > How to convert that to circular domain and assign only within the circular > domain.? > > Below is the script. > > translate([0, 0, -0.5]) > circle(14, $fn = 50); > > for (x = rands(-14, 14, 50), y = rands(-14, 14, 50)) > translate([x, y, 0]) cube([0.5, 0.5, 0.5]); > > I have attached the circle and obtained cube image for reference. > > <http://forum.openscad.org/file/t3044/circle.png> > > Image: Cubes on circle > > <http://forum.openscad.org/file/t3044/circle_cube.png> > > I do not want the cubes to go out of the circular domain. I tired working > with "pi" value and using cosine and sine, but could not been able to do > correctly. > > Any leads will be helpful. > > > Thanks and Regards, > > Sunag R A. > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > 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 |
There's a couple of more things that would need to be defined,
e.g. what happens with cubes at the border of the circle? Assuming the cubes are placed safely inside and no requirements regarding distribution, you could use random polar coordinates: radius = 14; size = 0.3; function pos(radius, size) = rands(0, radius - sqrt(2) * size / 2, 1)[0]; %cylinder(r = radius, h = 0.1); linear_extrude(size) for ([0:499]) let(a = rands(0, 360, 1)[0], r = pos(radius, size)) translate(r * [sin(a), cos(a)]) square(size, center = true); ciao, Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
|
Administrator
|
Or if you want to work-out whether they are inside or outside: translate([0, 0, -5]) color("blue",0.25) %circle(14, $fn = 50); for (x = rands(-14, 14, 5), y = rands(-14, 14, 5)) { dist=norm([x,y]); echo(x=x,y=y,dist=dist); translate([x, y, 0]) { translate([-0.5,1,-2])
color("black")
%text(str(floor(dist)),size=0.75); color((dist>14) ?
"red" : "green") cube([0.5, 0.5, 0.5]); } } Again you'd need to workout edge cases e.g. This is outside because the origin is outside: This is inside: > -----Original Message----- > From: Discuss [mailto:[hidden email]] On Behalf Of Torsten Paul > Sent: Tue, 5 Jan 2021 16:06 > To: [hidden email] > Subject: Re: [OpenSCAD] How to generate random cubes within the domain of circle radius > > There's a couple of more things that would need to be defined, > e.g. what happens with cubes at the border of the circle? > > Assuming the cubes are placed safely inside and no requirements > regarding distribution, you could use random polar coordinates: > > radius = 14; > size = 0.3; > > function pos(radius, size) = rands(0, radius - sqrt(2) * size / 2, 1)[0]; > > %cylinder(r = radius, h = 0.1); > > linear_extrude(size) > for ([0:499]) > let(a = rands(0, 360, 1)[0], r = pos(radius, size)) > translate(r * [sin(a), cos(a)]) > square(size, center = true); > > ciao, > Torsten. > > _______________________________________________ > OpenSCAD mailing list > 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
OpenSCAD Admin - email* me if you need anything, or if I've done something stupid...
* on the Forum, click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. |
In reply to this post by tp3
Thank you so much for the reply.
Firstly, I tried to implement your code and check the working method. But it is giving me a parse error in the "let" method of function. Is there anything I need to install or load to clear that error? I mean it is not taking "let" method. Thanks and Regards, Sunag R A. --------------- Original Message --------------- There's a couple of more things that would need to be defined, e.g. what happens with cubes at the border of the circle? Assuming the cubes are placed safely inside and no requirements regarding distribution, you could use random polar coordinates: radius = 14; size = 0.3; function pos(radius, size) = rands(0, radius - sqrt(2) * size / 2, 1)[0]; %cylinder(r = radius, h = 0.1); linear_extrude(size) for ([0:499]) let(a = rands(0, 360, 1)[0], r = pos(radius, size)) translate(r * [sin(a), cos(a)]) square(size, center = true); ciao, Torsten. -- 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 tp3
On 05.01.21 06:47, rasunag27 wrote:
> giving me a parse error in the "let" method of function That is working in release version 2019.05, are you running something older? ciao, Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
|
Oh.. I am working with 2015.03 - 1.
I will update the version and check. Thanks and Regards, Sunag R A. -- Sent from: http://forum.openscad.org/ _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
On 05.01.21 06:55, rasunag27 wrote:
> Oh.. I am working with 2015.03 - 1. Right, that's ancient even with the very slow release cycle of OpenSCAD ;-). The let is not really required for this case though, I think that should work with 2015.03: linear_extrude(size) { for ([0:499]) { a = rands(0, 360, 1)[0]; translate(pos(radius, size) * [sin(a), cos(a)]) square(size, center = true); } } ciao, Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
|
Yes..I will work without "let".
Since I tried to reinstall Openscad. As I am working in ubuntu, it is taking the version of 2015.03 - 1 only. It is not updating. Thanks and Regards, Sunag R A. -- 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 tp3
The code:
linear_extrude(size) { for ([0:499]) { a = rands(0, 360, 1)[0]; translate(pos(radius, size) * [sin(a), cos(a)]) square(size, center = true); } } is working fine. Now I can build upon this on what I needed other than cube. Thank you so much everyone. Regards, Sunag R A. -- 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 rasunag27
Thus rasunag27 hast written on Mon, Jan 04, 2021 at 11:06:32PM -0700, and, according to prophecy, it shall come to pass that:
> > Since I tried to reinstall Openscad. As I am working in ubuntu, it is taking > the version of 2015.03 - 1 only. It is not updating. Ubuntu 18.04.5 LTS You can install the snap with: sudo apt update sudo apt install snapd # If needed sudo snap install openscad # Version 2019.05 OR sudo snap install openscad-nightly # Version 2021.01.03 _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
This post was updated on .
In reply to this post by rasunag27
translate([0, 0, -0.5]) circle(14, $fn = 50); for (x = rands(-14, 14, 50), y = rands(-14, 14, 50)) if(norm ([x,y])<14-0.707107*0.5 ) translate([x, y, 0.25]) cube([0.5, 0.5, 0.5],center=true); Or a completely exact solution: translate([0, 0, -0.5]) circle(14, $fn = 50); for (x = rands(-14, 14, 50), y = rands(-14, 14, 50)) if( norm ([x-.25,y-.25])< 14 && norm ([x+.25,y-.25])< 14 && norm ([x+.25,y+.25])< 14 && norm ([x-.25,y+.25])< 14 ) translate([x, y, 0.25]) cube([0.5, 0.5, 0.5],center=true); ![]() |
Free forum by Nabble | Edit this page |