Solved

Finding bearing from two points.

Posted on 2001-07-03
7
1,119 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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