I have pondered this problem for several years now and think it to be impossible but I have to ask to ensure.
A rectangular room that has an arbitrary WIDTH, and HEIGHT. The goal is to place squares in the room randomly without any overlaps and without any of them being outside the room. Placement is not aligned to any grid. In addition each square placed should be uniformly randomly positioned in any valid place.
Think of: In the real world taking a handful of scrabble pieces and throwing them in a cardboard box.
A bad way I found to do this:
for( i=0; i < total_squares; i++){
rx = random(WIDTH); ry = random(HEIGHT);
if(no_collision(rx,ry)){
place_square(rx, ry);
}
else{
i--; //alternative_way i=0; unplace_all_squares();
}
}
This works if the room is sufficiently large, but I wanted it to work in a room that could barely fit each square. Then the loop could get stuck since there is no place valid place. An alternative is setting i to zero in the else and removing all the squares placed. That will work but could take nearly infinite time.
Another way is to have the square slide off overlaps but that biases the placement to close to other squares.
So I came to the conclusion I can't help the bias because of how collision biases the square to a position in the room and my vapid solution would have bias in it if every completed. But I am not sure.
I was using game maker for this but pseduo-code or any language would be fine.
Aucun commentaire:
Enregistrer un commentaire