1

I have an array containing images that I want displayed onto my view. I have a loop that goes through my array and picks a random image, then displays the image in a random position on my view. Each image has a "value" and the loop runs until a total "value" of 50 is reached.

My problem is, I do not want my images to touch each other or stack on top of each other. I have used google and youtube and various other coding help sites and for some reason cannot find a solution that fits my needs.


Code

-(void)randomizeImages {
    //get random number
    int randomImgNum = arc4random_uniform(5);

    //use random number to get an image from array
    UIImage *tempImg = [_imageArray objectAtIndex:randomImgNum];

    //add UIImage to a UIImageView and place it on screen somewhere
    UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];

    //define the center points
    tempImgView.center = CGPointMake(arc4random() % 320,arc4random() % 480);
    [self.view addSubview:tempImgView];


    //increment count
    myImgCount = myImgCount+(randomImgNum+1);

    //check count
    if (myImgCount<50) {
        [self randomizeImages];//do it again if not yet at 50
    }
}

I was going to include a screenshot but apparently I need 10 reputation before I can. I have searched everywhere for help, and posted this question a few days ago but the code that was suggested kept giving me a szone_malloc_should_clear + 14 error and I have no idea what this means or how to fix it. The question topic is dead and I really need help solving this issue.


Screen Shot

screen shot

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

You have to record all positions of generated images. Make an outer for loop with 50 iterations for your 50 pictures. Then make an inner loop that

  1. generates random x,y coordinates for new picture with number i
  2. loop trough all pictures from 0 to i-1 and check if new picture is not stacked on another, if it touches, loop repeats again, for a safe limit (lets assume 100 iterations)
  3. after you find out good coordinates, draw the picture and store your x,y coordinates to an array and exit the inner loop

Check this link for rectangle overlap detection. You can upgrade the algorithm with a simple trick so those rectangle won't get near each other... Alternatively google up how to check rectangle collision

  1. make new temporary variables for positions of their upper left and bottom right points and calculate them ( for example rect_1_bottom_right_point_x = x + rect_1_width )
  2. substract a margin both from upper left point x and y, add a margin to both bottom right x and y so they look larger for the testing
  3. now test those two rectangles for overlapping
Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
  • Im a pretty big noob to coding so I have a few questions. How do I record the positions/check if images are stacked in my loop? So what this loop does is record the random position it places an image, then when it goes to place another it checks to see if another image is too close, and keeps repeating until all images can be displayed? The loop I currently have, does that stay by itself or get put inside these new loops? Thanks for the help man – user2939895 Nov 03 '13 at 01:00
  • your current loop has a function of the outer loop in my algorithm, anyway you are using recursion call, i would use a for loop – nio Nov 03 '13 at 10:37
  • Im not sure if this is too much to ask, but are you able to help me create my nested for loop? I am having a difficult time even setting it up. – user2939895 Nov 03 '13 at 22:12