Drawing line between to different objects

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,
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];

void setup (){
  for (int i=0; i<sphares.length; i++){
    sphares[i] = new Sphares();

void draw (){

   for (int i=0; i<sphares.length; i++){

class Sphares{
  float x;
  float y;
  float d;
  float sw;
  float w;
  float s;
  void display(){
    x = x + w/9;
    y = y + s/9;


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 :slight_smile:

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

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

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.
      Don't do anything.

Can you see how they translate almost directly into code?

// 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.

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?

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:

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

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

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.

1 Like

Sorry :grimacing:, but i thought that he really needed a push


It felt like something was missing :grin:

1 Like

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

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

More info here:

1 Like