# How to generate random cubes within the domain of circle radius

12 messages
Open this post in threaded view
|

## How to generate random cubes within the domain of circle radius

 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. Image: Cubes on circle 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
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

Open this post in threaded view
|

## Re: 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 [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Torsten
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

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

>

> 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:

>

> 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.

>

> _______________________________________________

 Virus-free. www.avg.com

_______________________________________________
[hidden email]
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.
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

 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
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

 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
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

 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
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

 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
Open this post in threaded view
|

## Re: How to generate random cubes within the domain of circle radius

 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
 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); ```