Solved

Finding bearing from two points.

Posted on 2001-07-03
7
1,122 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Network game - UDP safety concerns? 10 292
Is an Indie RTS practical? 7 145
nearTen java challenge 8 105
Trigonometry for game star field help 1 140
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…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

738 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