discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

How to generate random cubes within the domain of circle radius

R
rasunag27
Tue, Jan 5, 2021 3:59 AM

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/

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/
BL
Bryan Lee
Tue, Jan 5, 2021 4:36 AM

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
Discuss@lists.openscad.org
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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
TP
Torsten Paul
Tue, Jan 5, 2021 5:05 AM

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.

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.
M
MichaelAtOz
Tue, Jan 5, 2021 5:24 AM

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:discuss-bounces@lists.openscad.org] On Behalf Of Torsten Paul

Sent: Tue, 5 Jan 2021 16:06

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

--
This email has been checked for viruses by AVG.
https://www.avg.com

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:discuss-bounces@lists.openscad.org] On Behalf Of Torsten Paul > Sent: Tue, 5 Jan 2021 16:06 > To: discuss@lists.openscad.org > 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- This email has been checked for viruses by AVG. https://www.avg.com
R
rasunag27
Tue, Jan 5, 2021 5:47 AM

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.

--
Sent from: http://forum.openscad.org/

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. -- Sent from: http://forum.openscad.org/
R
rasunag27
Tue, Jan 5, 2021 5:49 AM

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.

--
Sent from: http://forum.openscad.org/

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. -- Sent from: http://forum.openscad.org/
R
rasunag27
Tue, Jan 5, 2021 5:51 AM

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/

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/
TP
Torsten Paul
Tue, Jan 5, 2021 5:52 AM

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.

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.
R
rasunag27
Tue, Jan 5, 2021 5:55 AM

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/

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/
TP
Torsten Paul
Tue, Jan 5, 2021 6:00 AM

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.

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.