Solved

Finding bearing from two points.

Posted on 2001-07-03
7
1,114 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

18 Experts available now in Live!

Get 1:1 Help Now