We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Calculating distance between two objects
Page Index Toggle Pages: 1
Calculating distance between two objects (Read 1576 times)
Calculating distance between two objects
Jan 28th, 2010, 5:57am
 
I'd like to draw a line connecting the middle of each of the circles when they touch only. I have managed to make a simple method for highlighting each circle when they touch. I just don't know how to calculate and draw a line between these two objects when highlighted (touching). Any help would be much appreciated.

Here's the code:


Agents agent1;
 Agents agent2;
 float x;
 float y;
 float angle = 0.0; // Direction of motion  
 float speed = 1.4; // Speed of motion

void setup() {
 
 size(700, 400);
 background(255);
 smooth();
 frameRate(25);
 
 agent1 = new Agents(200,200,150,speed,angle);
 agent2 = new Agents(200,200,175,5,angle);
}

void draw() {

 background(255);
 agent1.move();
 agent2.move();
 
 if (agent1.nearanotherAgent(agent2)) {
 agent1.highlight();
 agent2.highlight();
 
 }

 agent1.display();
 agent2.display();
 
}

class Agents {
 
 float x,y;
 float diameter;
 float speed;
 float angle;
 color c = color(0);
 
 
 Agents(float xpos, float ypos, float dia, float sp, float dir) {
   
   x = xpos;
   y = ypos;
   diameter = dia;
   speed = sp;
   angle = dir;
   
 }
 
 
 void move() {
   
  angle += random(-0.13, 0.13);
  x += cos(angle) * speed;
  y += sin(angle) * speed;
 
    if(x >= width+diameter/2) {
       x = 0.0;
     }  

     if(y >= height+diameter/2) {
       y = 0.0;  
     }
     
     if(x <= 0.0-diameter/2) {
      x = width + diameter;
     }  
     
     if(y <= 0.0-diameter/2) {
      y = height;
     }
 }
 
 void highlight() {
   c = color(255,0,0);
   
 }
 
 void display() {
   
   stroke(c);
   noFill();
   ellipse(x,y,diameter,diameter);
   c = color(0);
   
 }
 
  boolean nearanotherAgent(Agents b) {
  return dist(x,y,b.x,b.y)<=diameter;
 }
}
Re: Calculating distance between two objects
Reply #1 - Jan 28th, 2010, 6:41am
 
Note: in general we avoid plural in class names unless they represent several objects (eg. a list of agents).
Instead of agent1.highlight(); agent2.highlight(); make a highlight() method taking an Agents parameter, call it with agent1.highlightTo(agent2); and there draw your line between the two centers.
Or, alternatively, if nearAnotherAgent is positive, store the other agent in a field.
Re: Calculating distance between two objects
Reply #2 - Jan 29th, 2010, 12:34am
 
Thank you for your reply and advice PhiLho. I'm still at a loss however on how to calculate the center of each ellipse and the distance between each, as well as keeping that information up to date  throughout the animation. I'm guessing that this information is what I need to call in the line's x,y coordinates and length variables? Any snippets of code may well help me out. Thank you.
Re: Calculating distance between two objects
Reply #3 - Jan 29th, 2010, 2:02am
 
ellipseMode(CENTER) is a good place to start: then your ellipse's x and y coordinates represent the centre of the ellipse.

As for calculating the distance between them...  Ever heard of Pythagoras's theorum  You can draw a right angled-triangle between any two points.  If in doubt this is often used for collision detection: a FAQ - i.e. a search will come up with plenty of answers.
Re: Calculating distance between two objects
Reply #4 - Jan 29th, 2010, 2:35am
 
Code:
Agent agent1;
Agent agent2;
float x;
float y;
float angle = 0.0; // Direction of motion  
float speed = 1.4; // Speed of motion

void setup() {

size(700, 400);
background(255);
smooth();
frameRate(25);

agent1 = new Agent(200,200,150,speed,angle);
agent2 = new Agent(200,200,175,5,angle);
}

void draw() {

background(255);
agent1.move();
agent2.move();

if (agent1.nearAnotherAgent(agent2)) {
  agent1.highlight();
  agent2.highlight();

}

agent1.display();
agent2.display();

}

class Agent {

float x,y;
float diameter;
float speed;
float angle;
color c = color(0);
Agent nearAgent;



Agent(float xpos, float ypos, float dia, float sp, float dir) {
 
  x = xpos;
  y = ypos;
  diameter = dia;
  speed = sp;
  angle = dir;
}

void move() {
 angle += random(-0.13, 0.13);
 x += cos(angle) * speed;
 y += sin(angle) * speed;

   if(x >= width+diameter/2) {
x = 0.0;
    }  

    if(y >= height+diameter/2) {
y = 0.0;  
    }
   
    if(x <= 0.0-diameter/2) {
x = width + diameter;
    }  
   
    if(y <= 0.0-diameter/2) {
y = height;
    }
}

void highlight() {
  c = color(255,0,0);
  if (nearAgent != null) {
    stroke(#FF0000);
    line(x, y, nearAgent.x, nearAgent.y);
  }
}


void display() {
  stroke(c);
  noFill();
  ellipse(x,y,diameter,diameter);
  c = color(0);
}

 boolean nearAnotherAgent(Agent b) {
   boolean near = dist(x,y,b.x,b.y) <= diameter;
   if (near) {
nearAgent = b;
   } else {
nearAgent = null;
   }
   return near;
}
}
Re: Calculating distance between two objects
Reply #5 - Jan 29th, 2010, 3:09am
 
Merci PhiLho ! Much appreciated.
Re: Calculating distance between two objects
Reply #6 - Jan 29th, 2010, 6:27am
 
Ok, I bet you knew this question was coming didn't you? Well, I don't really have a question here, rather I'm pulling my hair out trying to get my head around implementing an array of agents with this code. I don't understand what I'm doing wrong after many a search...clueless I am!


int numAgents = 5;

Agent[] ag = new Agent [numAgents];

float x;
float y;
float angle = 0.0; // Direction of motion  
float speed = 1.4; // Speed of motion
float diameter = 0.0;

void setup() {

size(700, 400);
background(255);
smooth();
frameRate(25);
//float rad = random(5.0,150.0);

for (int i = 0; i < ag.length; i++) {
ag[i] = new Agent(200,200,speed,angle);

}
}


void draw() {

background(255);
for (int i = 0; i < ag.length; i++) {

ag[i].move();

if (ag[i].nearAnotherAgent(ag[i])) {
  ag[i].highlight();
}
ag[i].display();

}
}


//--------------------------------------------------------------------------

class Agent {

float x,y;
float diameter;
float speed;
float angle;
color c = color(0);
Agent[] nearAgent;

//--------------------------------------------------------------------------


  Agent(float xpos, float ypos, float sp, float dir) {
 
  x = xpos;
  y = ypos;
  diameter = random(5.0,150.0);
  speed = sp;
  angle = dir;

}

//--------------------------------------------------------------------------

void move() {
 angle += random(-0.13, 0.13);
 x += cos(angle) * speed;
 y += sin(angle) * speed;

   if(x >= width+diameter/2) {
      x = 0.0;
    }  

    if(y >= height+diameter/2) {
      y = 0.0;  
    }
   
    if(x <= 0.0-diameter/2) {
     x = width + diameter;
    }  
   
    if(y <= 0.0-diameter/2) {
       y = height;
    }
}

//--------------------------------------------------------------------------

void highlight() {
  c = color(0,0,255);
  for (int i = 0; i < ag.length; i++) {
  if (nearAgent[i] != null) {
    stroke(c);
    line(x, y, nearAgent[i].x, nearAgent[i].y);
  }
}
 }

//--------------------------------------------------------------------------

void display() {
  stroke(c);
  noFill();
  ellipse(x,y,diameter,diameter);
  c = color(0);
}

//--------------------------------------------------------------------------

 boolean nearAnotherAgent(Agent b) {
   boolean near = dist(x,y,b.x,b.y) <= diameter;
    for (int i = 0; i < ag.length; i++) {
   if (near) {
       nearAgent[i] = b;
   }
   else {
     nearAgent[i] = null;
   }
   }
   return near;
 
 }
}
Re: Calculating distance between two objects
Reply #7 - Jan 29th, 2010, 8:22am
 
You don't initialize nearAgent array, do that in the constructor.
Also you do: if (ag[i].nearAnotherAgent(ag[i])) { ie. you check if an agent is near itself!
You must do a double loop, something like:
Code:
for (int i = 0; i < ag.length; i++) {
ag[i].move();
for (int j = i + 1; j < ag.length; j++) {
if (ag[i].nearAnotherAgent(ag[j])) {
ag[i].highlight();
}
}
}
(untested, some problems probably remain)
Re: Calculating distance between two objects
Reply #8 - Jan 29th, 2010, 9:41am
 
Ok I wasn't sure about that. So, I needed a double loop to check one with another - that makes sense. All is working fine now. Thank you once again!
Page Index Toggle Pages: 1