"Timo" wrote in message <ikqcik$t3e$1@fred.mathworks.com>...
> Hi Sean or Carlo,
>
> I have a similar 'problem' but with some additional preconditions;
>
> 1. particles may not overlap
> 2. the part of the particle that falls off the edge of the cube has to be positioned at the oposite edge in such a way that connecting multiple of the same cubes would lead to a continuous distribution of particles.
>
> I am looking for an efficient iteration procedure, hope you can help
> Timo
>
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <igklbh$3jg$1@fred.mathworks.com>...
> > "Carlo Ventura" <cventura86@gmail.com> wrote in message <igkjn0$idi$1@fred.mathworks.com>...
> > > I need to put a random distribution of spherical particles into a cube, to choose the position of each particle I'd like to use matlab.Someone knows how to do that or which algorithm is needed?
> > > Thanks!
> >
> > Pseudocode:
> > generate n random integers between 1 and numel(volume) where n is the number of spheres you want
> > generate n random numbers between 1 and the maximum sphere radius you want
> > loop through and create them using the formula for a sphere. It can be found on the FAQ here:
> > http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
> >
> > Good Luck!
> > Sean
Hi Timo,
I'm really busy today and won't be able to spend much time but here is route you could take, and we could hash out details next week if necessary:
Generate nrandom integers to be the centroids of your spheres.
Generate nrandom radii
Begin populating your volume one sphere at a time with it's radii.
Apply constraint 1: If the new sphere's radius overlaps with another sphere 2 options:
If new sphere overlaps others reduce it.
Remove this centroid completely and generate a new one/try again.
If you are looping through the centroids randomly, i.e. not starting at V(1,1,1) and traversing this will be truly random. If you're starting on one side/corner, that side will have bigger particles as the constraints encourage new spheres to be smaller.
Constraint 2 should be applied while you're creating the objects as well. If an object is on the edge, it can be determined easily (any(centroid  radius) < 1 OR any(centroid + radius >size(V)), where centroid is the [row col page] index of the centroid. You'll just need some fancy indexing tricks to place the sphere cap on the opposite side.
That will get you started; see how far you can get and post back with that.
Good Luck!
Sean
