Im trying to connect the balls to each other by lines but for some reason, im failing at it. What am i doing wrong?
int anz = 10;
int r = 8;
int speed = 3;
boolean show = true;
float tmp;
float x[] = new float[anz];
float y[] = new float[anz];
float sx[] = new float[anz];
float sy[] = new float[anz];
color c[] = new color[anz];
void setup()
{
size(500,500);
background(255);
noStroke();
// Alle Bälle an eine zufälliger Posiotion mit zufälliger Geschwindigkeit
for( int i = 0; i < anz; i++) {
show = true;
x[i] = random(r, width-r);
y[i] = random(r, height-r);
// BEGIN: Überlappungen verhindern
for(int k=0; k<i; k++) {
if( dist(x[k], y[k], x[i], y[i])<2*r ) {
show = false;
break;
}
}
if(show==false) {
i--;
continue;
}
// ENDE
sx[i] = random(-speed, speed);
sy[i] = random(-speed, speed);
if(sx[i]==0 && sy[i]==0) sx[i]=sy[i]=1;
c[i] = color(random(255), random(255), random(255));
ball(x[i],y[i],2*r,c[i]);
}
}
void draw()
{
background(255);
float dis;
for( int i = 0; i < anz; i++) {
// Kollision der Kugeln miteinander
for(int k=0; k<anz; k++) {
dis = dist(x[k], y[k], x[i], y[i]);
if( (dis>0 && dis<2*r) && k!=i) {
tmp = sx[i];
sx[i] = sx[k];
sx[k] = tmp;
tmp = sy[i];
sy[i] = sy[k];
sy[k] = tmp;
}
}
// Kollision mit der Wand
if( (x[i]<=r) || (x[i] >= width-r) ) sx[i] *= -1;
if( (y[i]<=r) || (y[i] >= height-r) ) sy[i] *= -1;
x[i] += sx[i];
y[i] += sy[i];
ball(x[i],y[i],r*2,c[i]);
line(x[i],y[i],r,c[i]);
stroke(0);
}
}
void ball(float x, float y, int d, color c) {
fill(c);
ellipse(x,y,d,d);
}