dimanche 2 octobre 2016

Filling a plane with random points

I would like to fill a plane with randomly placed points, check whether any of them overlap (and if they do, move one of them to empty place) and then calculate the average distance between them. Later I plan on extending that to 3D so that it is kind of having particles in a box. I know there must be better ways of doing it but here's what I came up with. For placing random points in a plane:

int pos[NUMBER][2];    /* Creates an array of NUMBER amount of points with x and y coordinate */
int a, b;
srand( time(NULL) );
for(a=0;a<NUMBER;a++)
      for(b=0;b<2;b++)
         pos[a][b]=rand()%11; /* Using modulus is random enough for now */

The next stage is finding points that over lap:

    for(a=0;a<NUMBER-1;a++)
      for(b=a+1;b<NUMBER;b++)
        if( pos[a][0] == pos[b][0] && pos[a][1] == pos[b][1])
           printf("These points overlap:\t", pos[a][0], pos[a][1]);

Now when I identify which points overlap I have to move one of them, but when I do the point in new position might overlap with one of the earlier ones. Is there any accepted way of solving this problem? One way is infinite while(true) loop with breaking condition but that seems very inefficient especially when system gets dense. Thank you!




Aucun commentaire:

Enregistrer un commentaire