# How can I connect balls to each other with a line?

**URL:** <https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470>\
**Category:** Coding Questions\
**Created:** [April 18, 2021, 3:19pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470 "2021-04-18T15:19:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [April 18, 2021, 3:19pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/1 "2021-04-18T15:19:40Z")

</div>

Im trying to connect the balls to each other by lines but for some reason, im failing at it. What am i doing wrong?

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 18, 2021, 3:23pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/2 "2021-04-18T15:23:34Z")

</div>

```auto
  if(i+1 < anz)
      line(x[i],y[i], x[i+1],y[i+1] );

```

and try

```
stroke(c[i]);
```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 18, 2021, 3:30pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/3 "2021-04-18T15:30:52Z")

</div>

Or this:

```auto
for (int j = 0; j < i - 1; j++) {
  line(x[i], y[i], x[j], y[j]);
}

```

Not sure if you want all the connections or simply the connection to the one before.

---

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [April 18, 2021, 3:32pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/4 "2021-04-18T15:32:22Z")

</div>

thanks! i however dont want the connections to the ‘top’

---

<div class="post-metadata">

**Author:** ![iox007](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@iox007](https://discourse.processing.org/u/iox007)\
**Post date:** [April 18, 2021, 3:32pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/5 "2021-04-18T15:32:55Z")

</div>

its kinda doesnt work. hmmm

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 18, 2021, 3:33pm UTC](https://discourse.processing.org/t/how-can-i-connect-balls-to-each-other-with-a-line/29470/6 "2021-04-18T15:33:11Z")

</div>

show your entire code then - how can I see what you did wrong?  
I tested it here, it works fine

> [@iox007](#):
>
> its kinda doesnt work. hmmm

What doesn’t work?

What happens and what do you want to happen instead?

Your post is not precise.

(my code draws just a line from a ball to the next)
