Solved

Finding bearing from two points.

Posted on 2001-07-03
7
1,125 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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