# Drawing line between to different objects

**URL:** <https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 29, 2018, 8:07pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185 "2018-07-29T20:07:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)\
**Post date:** [July 29, 2018, 8:07pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/1 "2018-07-29T20:07:27Z")

</div>

Hello everyone, I have object stars and what i need to do,

draw line between two stars, if two stars close to each other, and delete line if not,  
star(x,y);  
for example if the distance between star 1 x and star 2 x closer then 100 draw line between them,  
if not, do nothing.

Sphares [] sphares = new Sphares [50];

```auto
void setup (){
  
  size(720,380);
  for (int i=0; i<sphares.length; i++){
    sphares[i] = new Sphares();
  }
  
  
}

void draw (){
    background(20,208,255);

   for (int i=0; i<sphares.length; i++){
  sphares[i].display();  
}
}

class Sphares{
  
  float x;
  float y;
  float d;
  float sw;
  float w;
  float s;
Sphares(){
  
  x=random(0,width);
  y=random(0,height);
  d=random(-5,5);
  sw=random(1,3);
  w=random(-5,5);
  s=random(-5,5);
  
}
  
  void display(){
    
    stroke(255,250,230);
    strokeWeight(sw);
    point(x,y);
    x = x + w/9;
    y = y + s/9;
    
    
  }
}

```

---

<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:** [July 29, 2018, 8:11pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/2 "2018-07-29T20:11:52Z")

</div>

Hi,

Can you edit your post and format your code using `</>` please?

What is the problem that you are facing with what you want to do:

- Getting the distance?
- Drawing the lines?
- …?

You need to be more precise if you want us to help you 🙂

---

<div class="post-metadata">

**Author:** ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)\
**Post date:** [July 29, 2018, 9:26pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/3 "2018-07-29T21:26:07Z")

</div>

Ow, I just found how to write codes in the forum, sorry =)

there are two points  
one starts from right side, other one starts from left side  
during they are close each other like 100px  
A line appears between them,  
line stays until the distance is larger then 100 px.  
and then line will disappears

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 29, 2018, 9:45pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/4 "2018-07-29T21:45:44Z")

</div>

Ok. So these are the steps you’ll want to follow.

```nohighlight
For each point,
  For every point after that point,
    Find the distance between those points.
    If the distance is less than 100,
      Draw a line between the points.
    Otherwise,
      Don't do anything.

```

Can you see how they translate almost directly into code?

```auto
// For each point,
for( ???; ???; i++ ) {
  // For every point after that point,
  for( int j = i+1; ???; ??? ){
    // Find the distance between those points.
    float distance = dist( sphares[i].x, ???, ???, ??? );
    // If the distance is less than 100,
    if( ??? < ??? ){
      // Draw a line between the points.
      line( ???, ???, ???, ??? );
    // Otherwise,
    } //else {
      // Don't do anything.
    //}
  }
}

```

---

<div class="post-metadata">

**Author:** ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)\
**Post date:** [July 30, 2018, 11:08am UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/5 "2018-07-30T11:08:35Z")

</div>

i can’t see dude =) my ignorance and those question marks creates a little dark hole in universe. What are they can you explane a little bit more if you have time?

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [July 30, 2018, 11:31am UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/6 "2018-07-30T11:31:00Z")

</div>

what you need to do is compare each point with all the others, eg. if you have 3 points:

name x y  
point1 10 50  
point2 110 51  
point3 10 20

then you have to compare point1 with point2 and point3, so the distance between point1 and point2 is greater than 100, but the distance between point1 and point3 is less than 100, so you can draw a line with point1 as start and point3 as end, then you take point2 and compare to point1 (wich is greater than 100) and with point3 (witch is also greater than 100), s you do nothing, then you take point3 and compare it with point1 where you have to draw a line (because the distance is less than 100) and then you compare with point2.  
This metod is inefficient but works (once that you understand you can be worried about efficiency).  
what @TfGuy44 tould you is that:

```auto
for( int i = 0; i < sphares.length; i++ ) {
  // For every point after that point,
  for( int j = i+1; j < sphares.length; j++ ){
    // Find the distance between those points.
    float distance = dist( sphares[i].x, sphares[i].y, sphares[j].x, sphares[j].y );
    // If the distance is less than 100,
    if( distance < 100 ){
      // Draw a line between the points.
      line( sphares[i].x, sphares[i].y, sphares[j].x, sphares[j].y );
    // Otherwise,
    } //else {
      // Don't do anything.
    //}
  }
}

```

btw, what @TfGuy44 give yo is a more efficient way to do it because it doesn’t compare twice the same thing

---

<div class="post-metadata">

**Author:** ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)\
**Post date:** [July 30, 2018, 11:54am UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/7 "2018-07-30T11:54:05Z")

</div>

Thank you both, I will exemine this for a while, btw i put it in the code and it worked very good. thanks again

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 30, 2018, 1:20pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/8 "2018-07-30T13:20:27Z")

</div>

The idea was that you would look at the structure of the code, and the steps, and work out how to fill the gaps in by yourself. Plus it’s easier to type `???` than `spahers[j].y`.

But anyway, it looks like someone has filled in the holes for you now.

Make sure you understand the code you have been given before you try to use it! This gift is a blessing and a curse. If your next post asking for help demonstrates to the forum regulars that you are a code thief and not a student gaining understanding, there will surely be a reckoning.

---

<div class="post-metadata">

**Author:** ![erwrow](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@erwrow](https://discourse.processing.org/u/erwrow)\
**Post date:** [July 30, 2018, 1:23pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/9 "2018-07-30T13:23:35Z")

</div>

> [@TfGuy44](#):
>
> But anyway, it looks like someone has filled in the holes for you now.

Sorry 😬, but i thought that he really needed a push

---

<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:** [July 30, 2018, 1:49pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/10 "2018-07-30T13:49:55Z")

</div>

> [@TfGuy44](#):
>
> Make sure you understand the code you have been given before you try to use it! This gift is a blessing and a curse. If your next post asking for help demonstrates to the forum regulars that you are a code thief and not a student gaining understanding, there will surely be a reckoning.

😈😈😈😈😈

It felt like something was missing 😁

---

<div class="post-metadata">

**Author:** ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)\
**Post date:** [July 30, 2018, 8:02pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/11 "2018-07-30T20:02:14Z")

</div>

I don’t know how to react this comments. But i feel if i do not comment it would be rude as well. Hopefully i don’t even understand why are you thinking like that and how do you see that i didnt try to do before asking. I don’t know which country you live in but here it is really hard to find someone to ask questions about processing. to learn i am watchin that crazy man everyday =) and of course my next question will be about a code that i couldnt write. I am trying to create a music video for others and thats it, I don’t see any thief here, just assumptions without true information, Thank you all btw, I will continue to ask questions, sorry my english thou. Stay peace

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 30, 2018, 8:10pm UTC](https://discourse.processing.org/t/drawing-line-between-to-different-objects/2185/12 "2018-07-30T20:10:04Z")

</div>

@erwrow Please do not provide full code solutions, especially to homework questions.

More info here:

> [@Guidelines—Answering Questions](https://discourse.processing.org/t/guidelines-how-to-answer-questions/2145):
>
> If you’re here, it means you’re thinking about contributing to the Processing community by answering questions on the forum. That’s great! This guide is meant to help you answer questions in a way that’s most helpful. Be nice. Remember that you’re talking to a person! Follow the guidelines in the [FAQ](https://discourse.processing.org/faq). Also read through the [P5.js community guidelines](https://p5js.org/community/). Specifically, avoid making judgments like “this code is bad” or “that’s a dumb way to do this”. Be mindful of how your tone comes across over the…
