Solved

Finding bearing from two points.

Posted on 2001-07-03
7
1,115 Views
Last Modified: 2012-06-21

 Assume I have two points:

 p1 = x1, y1
 p2 = x2, y2

 I need to determine the DIRECTION/bearing (in degrees) of point p2 from point p1.  (The answer will of course be between 0 and 360).

 for example, if p1 = 0,0 and p2 = 5,5, it would be 45 degrees (Er, I think hehe).

Anyway, help is appreciated.  Psuedocode (java or c) would be helpful rather than just a bunch of theory.

Thanks!


 
0
Comment
Question by:Eltmon
  • 4
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
HDE226868 earned 300 total points
ID: 6251344
To calculate direction:

Starting point is p1
Ending point is p2
Angle is measured anticlockwise
0 degrees is straight right

Pseudocode example:

// First You calculate distances (must be in signed variables)

dx=x2-x1
dy=y2-y1

// If x part is 0 You could get into division by zero problems, but in that case result can only be 90 or 270:

if dx=0 {
   if dy > 0
     result = 90
   else
     result = 270
   }
else {

// otherwise angle is determined as inverse tangent of coordinates

   result = (atan(dy / dx)) * 180 / pi
}

// the *180 / pi part is because results are usualy in radians, but You wanted it in degrees

// this is only valid for two quadrants (for right side of the coordinate system) so modify result:

if dx < 0 {
   result = result + 180
}

// looks better if all numberes are positive (0 to 360 range)

if result < 0 {
   result = result + 360
}
0
 
LVL 3

Expert Comment

by:HDE226868
ID: 6251349
In c/c++ You can also use atan2 function, which should give it all without all that if sentences. But to understand, just go through the whole process as described before.
0
 

Author Comment

by:Eltmon
ID: 6251376
THANKS that worked PERFECTLY.... I was actually close but I had some misunderstandings that you cleared up.

Excellent job, THANKS!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Eltmon
ID: 6251381
THANKS that worked PERFECTLY.... I was actually close but I had some misunderstandings that you cleared up.

Excellent job, THANKS!
0
 

Author Comment

by:Eltmon
ID: 6251382
How can I modify to use atan2 function?  
0
 
LVL 3

Expert Comment

by:HDE226868
ID: 6251408
double atan2( double y, double x );

I never used it before, but it seems it handles all that zero and quadrant handling. Should work like this:

result = (atan2(dy , dx)) * 180 / pi

if result < 0 {
  result = result + 360
}




From msdn:

atan returns the arctangent of x. atan2 returns the arctangent of y/x. If x is 0, atan returns 0. If both parameters of atan2 are 0, the function returns 0. You can modify error handling by using the _matherr routine. atan returns a value in the range ??/2 to ?/2 radians; atan2 returns a value in the range ?? to ? radians, using the signs of both parameters to determine the quadrant of the return value.

The atan function calculates the arctangent of x. atan2 calculates the arctangent of y/x. atan2 is well defined for every point other than the origin, even if x equals 0 and y does not equal 0.

0
 

Author Comment

by:Eltmon
ID: 6251422
Thanks once again!  I noticed in your bio that you work on game 3d stuff on the side.  I've been working on the same game for 5 years, and even have a Java3D client that is semi-working for it. It supports multiple types of play simultaneously, you can telnet to it like a mud, use a 3d client or 2d client, and its basically an MMRPG like asherons call or everquest.  Email me at Eltmon@yahoo.com if you are interested in learning more.

There is a screenshot of the first prototype of the 3d client at http://clubs.yahoo.com/clubs/mituen

It was about as far as I could get without advanced calculus.  the terrain generation system uses brownian motion with midpoint displacement for a virtually HUGE world. I'm surprised I got as far as I did with it, but I've been sticking to the backend stuff lately ;-)  I hope to put what I've got (which is a lot) up at mituen.sourceforge.net soon.  (BTW, the NPCs use real genetic programming and neural nets)

Id love to hear from you,and thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now