We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › image interaction
Page Index Toggle Pages: 1
image interaction (Read 485 times)
image interaction
Jan 30th, 2010, 3:21pm
 
Hello,
So i'm making a program that each time it runs it draws out a new 'collage.' I've loaded some images and each time it's run it's supposed to make the collage different (i.e. different locations for the items that are my images). I've got it working so the different types of items don't overlap whenever the program is run, but is there a way I can make it so that the images aren't drawn on top of each other? I'm drawing all the images through and array also. I tried using collision detection for the images, but since it's supposed to be 'random' i didn't know how to apply collisions to images from the array. Here is my code so far: Code:

PImage [] items = new PImage[12];
PImage background1;
PImage background2;
float rx, ry, tn;


void setup(){
 size(640, 480);
 background1 = loadImage("background1.png");
 background2 = loadImage("background2.png");
 
 for (int i = 0; i < items.length; i++){
   String imageName = "item-" + nf(i+1, 3) + ".png";
   items[i] = loadImage(imageName);
 }
 if(random(0, 1) < 0.5){
   background(background1);
 }else{
   background(background2);
 }
 noLoop();
}

void draw(){
 
 tn = random(230, 255);
 
 //onigri
 for (int i = 0; i < 3; i++){
     if(random(0, 1) < 0.5){
       rx = random(530, 585);
       ry = random(120, 320);
       //tint(255, tn);
       image(items[i], rx, ry);
       
       if(random(0, 1) > 0.5){
         rx = random(15, 75);
         ry = random(255, 320);
         image(items[i], rx, ry);
       }
    }
 }
 
 //ice cream
 for (int j = 3; (j < 5)&&(j > 2); j++){
     if(random(0, 1) < 0.5){
       rx = random(300, 440);
       ry = random(235, 360);
       image(items[j], rx, ry);
       
       /*if(random(0, 1) > 0.5){
         image(items[j], rx + 30, ry + 70);
       }*/
   }
 }
 
 //batteries
 for (int k = 6; (k < 8)&&(k > 5); k++){
     if(random(0, 1) < 0.5){
       rx = random(160, 210);
       ry = random(240, 360);
       image(items[k], rx, ry);
       
       /*if(random(0, 1) > 0.5){
         image(items[k], rx+20, ry+70);
       }*/
   }
 }
 
 //sushi
 for (int l = 9; (l < 11)&&(l > 8); l++){
   if(random(0, 1) < 0.5){
     rx = random(25, 370);
     ry = random(125, 170);
     image(items[l], rx, ry);
   }
 }

Thanks for any help!




Re: image interaction
Reply #1 - Jan 31st, 2010, 1:55am
 
It is the good old problem of packing objects in a limited space. It is among the hard problems in computing!
Now, there can be simple, practical heuristics, particularly if available space is much bigger than the total size of the objects...
One of these solutions could be to pick up one object at random, put it near one side, then put the next random object near it, etc. You can make a coarse or tight virtual grid, each object filling a number of grid cells. You can then explore that grid to see if there is a big enough space to put next object, trying to avoid to fragment it too much.
Page Index Toggle Pages: 1