Tech Support Forum banner
Status
Not open for further replies.
1 - 10 of 10 Posts

·
Registered
Joined
·
4 Posts
Discussion Starter · #1 · (Edited)
Hi there!

I am trying to get this written out in code but I having a bit of trouble. i wonder if anyone can point me in the right direction.

I have 4 agents with (x,y) coordinate system. What I want to do is calculate the distance difference among the 4 agents, taking reference from agent 1 first and calculate the distance difference among agents 2 and 3 and 4 then taking reference from agent 2, 3 and finally agent 4.

A way getting through would be to use 2 for loops.

for a=1:4
for b=1:4
for a~=b

differences in distance of x and y with respect from 1 and 2, 3 and finally 4.
.
.
.

I know I will need to use indexing to store the differences but I cant seem to get it down.

Any help will be greatly appreciated! Thank you.
 

·
TSF-Emeritus
Joined
·
2,057 Posts
Hello skss, welcome to TSF!

This seems fairly easy to solve. Your basically wanting to get the distance between two agents. Let's say that:

Agent 1: (4,7)
Agent 2: (17,3)

First you would create a function/call that would get the x value of both agents, then compare the two values to find the largest one. It then takes the largest of the values and subtracts the smaller value from it. Saving the difference between the two into an integer-type variable. Do this same process for the y value of both agents.

If by chance the two values are the same, you can just make it instantly call a value of zero, meaning they have the exact same value and are at the same point.
 

·
Registered
Joined
·
4 Posts
Discussion Starter · #3 ·
Hi Ninjaboi! Thank you for the welcome! And thank you for your reply.

What if I have 5 agents and want to store the differences with respect to agent 1 for example in c so that I can use these values later in the programme?

I am assuming I will need to define c as a matrix? Is that right?
 

·
Registered
Joined
·
4 Posts
Discussion Starter · #4 ·
For example I have this:
c=1:1:6;
x=[0.5 2 3];
y=[1 2 4];
for a=1:3
for b=1:3
if a~=b

diff_x(1,c)=x(1,a)-x(1,b)
end
end
end

However I do not get what I want. What I want is diff_x to store the answer in this form -1.5 -2.5 1.5 -1 2.5 1. So what am I doing wrong?
 

·
TSF-Emeritus
Joined
·
2,057 Posts
Ah, so you do want negative values to be expressed am I correct? Then you could try this ( not sure what language your using, so I'll just do C/C++. Also, I'm not at a terminal that I can compile this code, so I'm going to keep it very simple and hopefully no errors will occur. ):

Code:
#include <iostream>
 
int main()
{
 
     int agent1x = 4;
     int agent1y = 6;
 
     int agent2x = -2;
     int agent2y = 13;
 
     int agent3x = 2;
     int agent3y = 7;
 
     int agent4x = 9;
     int agent4y = -8;
 
     int agent5x = 4;
     int agent5y = -5;
 
     int agentdiff12x = Difference( agent1x, agent2x);
     int agentdiff12y = Difference( agent1y, agent2y);
     int agentdiff13x = Difference( agent1x, agent3x);
     int agentdiff12y = Difference( agent1y, agent3y);
     int agentdiff14x = Difference( agent1x, agent4x);
     int agentdiff12y = Difference( agent1y, agent4y);
     int agentdiff15x = Difference( agent1x, agent5x);
     int agentdiff12y = Difference( agent1y, agent5y);
 
     std::cout << "Differences: " << std::endl << std::endl;
 
     std::cout << agentdiff12x << "," << agentdiff12y << std::endl;
     std::cout << agentdiff13x << "," << agentdiff13y << std::endl;
     std::cout << agentdiff14x << "," << agentdiff14y << std::endl;
     std::cout << agentdiff15x << "," << agentdiff15y << std::endl;
 
}
 
int Difference(int agenta, int agentb)
{
 
     int result;
 
     result = agenta - agentb;
 
     return result;
 
}
Note that this is not the best way to represent this in code, but since I'm not able to compile, I had to keep it simple. Hope that helps a bit.
 

·
TSF-Emeritus
Joined
·
2,057 Posts
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE][/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Difference([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agenta, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentb);[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()[/SIZE]
[SIZE=2]{[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent1x = 4;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent1y = 6;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent2x = -2;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent2y = 13;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent3x = 2;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent3y = 7;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent4x = 9;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent4y = -8;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent5x = 4;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agent5y = -5;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff12x = Difference( agent1x, agent2x);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff12y = Difference( agent1y, agent2y);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff13x = Difference( agent1x, agent3x);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff13y = Difference( agent1y, agent3y);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff14x = Difference( agent1x, agent4x);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff14y = Difference( agent1y, agent4y);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff15x = Difference( agent1x, agent5x);[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentdiff15y = Difference( agent1y, agent5y);[/SIZE]
 
[SIZE=2]  std::cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Differences: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << std::endl << std::endl;[/SIZE]
 
[SIZE=2]  std::cout << agentdiff12x << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << agentdiff12y << std::endl;[/SIZE]
[SIZE=2]  std::cout << agentdiff13x << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << agentdiff13y << std::endl;[/SIZE]
[SIZE=2]  std::cout << agentdiff14x << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << agentdiff14y << std::endl;[/SIZE]
[SIZE=2]  std::cout << agentdiff15x << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]","[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] << agentdiff15y << std::endl;[/SIZE]
 
[SIZE=2]  std::cin.get();[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
 
[SIZE=2]}[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Difference([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agenta, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] agentb)[/SIZE]
[SIZE=2]{[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] result;[/SIZE]
 
[SIZE=2]  result = agenta - agentb;[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] result;[/SIZE]
 
[SIZE=2]}[/SIZE]
That is the revised version of the code I provided above. I have fixed and compiled it without a problem, and have made it "Copy + Paste" friendly. Again it's a simple example ( didn't redo it, just fixed any errors you'd of gotten from the previous code ) but it serves the purpose none the less. Hopefully however you don't use that for larger projects though lol.

Hope that helps! Happy coding!
 

·
Registered
Joined
·
1,930 Posts
Hi

Remember the distance (or length) between 2 points in a Cartesian plane is given by:
PointA (x1, y1)
Point2 (x2, y2)
Then:
Code:
Distance = sqrt( pow((x2-x1),2) + pow((y2-y1),2) )
OR
Distance = sqrt( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)) )
If you use sqrt and pow I believe you have to use #include <math.h>

Further more because Distance is a Scalar (not a vector) it only has magnitude and not direction. Therefor distance will always be positive no matter what the coordinates. This becomes obvious if you look at the formula since squaring any number (even negative number) will always give you a positive number. Just a little mathematics lesson:grin:
 

·
TSF-Emeritus
Joined
·
2,057 Posts
Further more because Distance is a Scalar (not a vector)
That's why I was a bit bewildered that skss wanted negative values to be in the application. However, I figured that skss was wanting their location relative to the location of agent 1 ( not necessarily distance ) so I didn't complain lol.
 

·
Registered
Joined
·
4 Posts
Discussion Starter · #9 ·
Hi AlbertMC2. Yup sorry if it did sounded confusing. Maybe should have used displacement instead. But anyways thank you for the input!

Ninjaboi! Thank you for the help! Must have been a pain typing them out 1 at a time. I was wondering if the format and sequencing of the programme will also work in matlab? I should have mentioned earlier so wont be needing to answer this. Many apologies.
 

·
TSF-Emeritus
Joined
·
2,057 Posts
We'll if by format you mean syntax then no. However, if by format you mean organization then yes. The program can be done in matlab just fine actually, it just won't have the same syntax as this lol. It will also probably be a bit easier due to this being a math problem and that's a math problem-solving language.

I've never used Matlab, but I've seen problems here on the forum ( so I understand the fundamentals of the syntax/approach ). I wouldn't be much help if your trying to use that language, so my apologies.
 
1 - 10 of 10 Posts
Status
Not open for further replies.
Top