Hi all,
I've been working on an app that involves placing multiple small CCSprites (boxElements) within a larger CCSprite (box) (i.e. multiple elements within a big box). I am able to determine a random location to position the elements within the box accurately, but the problem is I would like these elements to be fairly spaced out with the least amount of intersections as possible. In order to this, I am choosing a random position for each element in my box and then comparing its positions to the other elements in the box. If the position of the current element intersects with another element in the box within a 30% radius of that element, then I would like to choose another random location for my current element. The problem I am having is as follows: after my current element's position finds that it intersects with another element in the box, it chooses a new random location for my current element, but I'm not sure what the best way is for me to make sure that this new position doesn't intersect with any other element in the box that has either already been checked or needs to be checked with. I've included a portion of my code here that shows the checking, but it needs to be more precise and efficient with making sure that and old or new locations also don't intersect with elements in the box.
'
int randomX = *random position within box*;
int randomY = *random position within box*
for(int j =0; j < [boxElements count]; j++) {
CCSprite *tempS = [boxElements objectAtIndex:j];
int xOffset = (int)0.3 *(tempS.scale * tempS.contentSize.width);
int yOffset = (int)0.3 *(tempS.scale * tempS.contentSize.height);
CGPoint tempRand = ccp(randomX, randomY);
//check if the box contains the point and if the point is within 30% from the center, then choose another point
if(CGRectContainsPoint(tempS.boundingBox, tempRand) && (tempRand.x < (tempS.position.x +xOffset)) && (tempRand.x > (tempS.position.x - xOffset)) && (tempRand.y < (tempS.position.y +yOffset)) &&
(tempRand.y > (tempS.position.y - yOffset))) {
randomX = *new location within box*
randomY = *new location within box*
}
}
'
Also, one thing to note is that each element is a different size.
Thank you for any suggestions in advance.