topical media & game development
graphic-processing-algorithm-Appendix-p325-3-p325-3.pde / pde
String [] cube = new String[35];
int index=0;
int space[] = {
7,2,4,6,6,5,5 }; //coverage of each room
int space_name[] = {
4,5,1,2,3,6,7 };
color [] colors = new color[space.length];
void setup(){
size(530,400);
background(255);
for(int i=0; i<colors.length; i++)
colors[i] = color(random(255),random(255),random(255));
}
void draw(){
}
void mousePressed(){
index = 0;
}
void keyPressed(){
if(index==35)return;
int done=0;
background(255);
index = 0;
//All spaces with constraints
int xr = (int)random(7); //get a random integer 0 to 5 for x to start
int yr = (int)random(5); //get a random integer 0 to 7 for y
int xp = xr;
int yp = yr;
for(int i=0; i<space.length; i++){
int numCubes=0;
int k=0;
while(numCubes < space[i]){
xp = xr;
yp = yr;
if(random(1)<.5)
xr = xr + (int)random(-2,2); //get a random integer increment of 1 in x
else
yr = yr + (int)random(-2,2); //get a random integer increment of 1 in y
xr = constrain(xr, 0,6);
yr = constrain(yr, 0,4);
boolean exists = false;
for(int j=0; j<index; j++)
if(cube[j].equals("MyCube" + xr + "x" + yr))
exists = true;
if(exists==false){ //if there is nothing there
cube[index] = ("MyCube" + xr + "x" + yr);
//show the newly created cube
fill(colors[space_name[i]-1]);
rect(70+xr*50, height-(100+yr*50),50,50);
numCubes++;
index++;
}//if
else{
xr = xp;
yr = yp;
}
if((k++)>100){
done = 1;
break;
} //safety valve
}//while
if(done==1)break;
}//for
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.