topical media & game development
graphic-processing-algorithm-Ch07-p160-p160.pde / pde
float [] xp = new float[0];
float [] yp = new float[0];
int numObjects = 0;
int tol2 = 15;
void setup(){
size(300,300);
xp = append(xp,random(10,width-10));
yp = append(yp,random(10,height-10));
}
void draw(){
background(255);
for(int i=0; i<xp.length; i++)
rect(xp[i],yp[i],10,10);
}
void keyPressed(){
int k = 0;
while(true){
boolean overlap = false;
float xrand = random(xp[xp.length-1]-tol2,xp[xp.length-1]+tol2);
float yrand = random(yp[yp.length-1]-tol2,yp[yp.length-1]+tol2);
for(int j=0; j<xp.length; j++){
float distance = dist(xrand,yrand,xp[j],yp[j]);
if(distance < 10 || xrand>width-10 || xrand<1 || yrand >height-10 || yrand<1) overlap = true;
}
if(overlap==false){
xp = append(xp,xrand);
yp = append(yp,yrand);
tol2 = 15;
break;
}
k++;
if(k>1000){
//println(xp.length + " impass");
tol2+=10;
break;
}
}
//println(numObjects++);
//save("stoch"+nf(numObjects,2)+".jpg");
}
(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.