1,105,263 Community Members

get direction between 2 points ?

Member Avatar
wwsoft
Light Poster
29 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hello , I need to know how to get the direction from point A to point B

eg. point_direction(0 , 0 , 100,100); would equal 315
x1 y1 x2 y2

how do I compute this ?

Thanks !

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

Are you looking for the bearing? You would need to use the inverse trig functions found in the <cmath> header then convert the answers from radians to degrees.

Member Avatar
wwsoft
Light Poster
29 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Are you looking for the bearing? You would need to use the inverse trig functions found in the <cmath> header then convert the answers from radians to degrees.

what would the code be to this ?

Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
0
 
#include <cmath>
#include <iostream>

int main()
{
  double x1 = 0;
  double y1 = 0;
  double x2 = 100;
  double y2 = 100;
  
  std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl;
  
  return 0;
}

The answer is 45 not 315 going from (0,0), to (100,100), right?

Dave

Member Avatar
rectifryer
Newbie Poster
19 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Just look up the distance formula.

Member Avatar
wwsoft
Light Poster
29 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#include <cmath>
#include <iostream>

int main()
{
  double x1 = 0;
  double y1 = 0;
  double x2 = 100;
  double y2 = 100;
  
  std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl;
  
  return 0;
}

The answer is 45 not 315 going from (0,0), to (100,100), right?

Dave

well in graphics everything starts at the top left hand side :icon_smile:

Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
0
 

Ah, yes. In mathematics the standard is +x is 0 degrees and the angle increases counter-clockwise. You'll have to adjust accordingly.

Dave

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article