Click to See Complete Forum and Search --> : Calculating largest "open" rectangular area?

03-26-2002, 10:37 AM
I have a directory of photographs (compiled as .swf's) that I'm dynamically loading and displaying on a 600x800 screen. When created, each fades itself in, then back out, then creates a new instance and starts the process for the next photo before self-destructing. About 5 to 8 are on screen at any given time.

I want these to continue to position themselves at random points on the screen so that each time the movie is played, different photos are loaded and also positioned differently. The problem is that as they are positioned randomly, they sometimes overlap.

If I register the x/y coordinates for each rectangle as it's created, as well as it's width/height, there should be some means of determining where the largest "empty" are on the screen is and then centering the next photo in the middle of this area. Without a strong math or CS background, I'm having trouble doing this.

Would anyone mind telling me how you'd approach this in Flash 5?



03-26-2002, 06:42 PM
Interesting problem.

Before the photo fades in, have a few frames where it's completely invisible. Before it appears, check to see if it's colliding with another photo. If it is, then take it out and replace randomly again until it doesn't overlap.

In effect, rather than working out where it should go, you're moving it around until it isnt hitting anything else.

It doesn't answer the question of how to calculate the largest empty area, but it does answer the question of how I'd approach the problem. :D

03-26-2002, 06:47 PM
Thanks for the suggestion, Lesli.

Yes, that's exactly the approach (collision detection) I was planning to take if no one had a formula for the largest open space. I'll see how it looks.

The only thing I was concerned about is that it would be very easy for pictures to be barely not overlapping, and thus be all crowded over in the corner with a vast open area remaining on the page. In that case people would wonder why I was too lazy to distribute them better, and I'd lose the effect I'm shooting for (recalling old memories).

Thanks again,


03-26-2002, 07:16 PM
You could of course divide the screen up into sections (for argument's sake, lets say four quadrants) And have the picture's appear in a random position within a random quadrant. That would give a random appearance and also force the pictures to space out around the screen.

Another possible solution to having pictures occupying one corner of the screen would be to have them start in the centre and filter outwards in random directions until they hit a 'clean' spot.

In fact, don't use a random direction, increment using an angle that won't repeat too often, like 150 degrees.