Sunday, August 2, 2009

How do I find out the bearing between to points in a C++ program?

How do I find out the bearing between to points in a C++ program?





I need to find out the bearing (relative to north) in a C++ program.





For example, if point 1 is at x5,y5 and point 2 is at x0,y0 the answer I want to get is that the bearing FROM point 1 TO point 2 is 225 degrees.





Y


|


| --------------1


|


|


|2____________X





Does anyone know how to find this?

How do I find out the bearing between to points in a C++ program?
You need to use what's called the Dot Product. The Dot product gives the angle between two vectors, so you would want your up or "north" vector to be 0, 1. That's a vector that points up (positive y).





To get a direction vector pointing from P2 to P1, subtract (P1 - P2). That will give you a new direction vector pointing from P2 to P1.





We will call our direction vector to our P1 point dirVec, and the up vector....up. (I am so creative)





To get the Dot product = up.x * dirVec.x + up.y * dirVec.y





Bingo, you now have the angle.





With different things working with angles, you may have to convert from radians to degrees depending what you are doing;





To do that: float degrees = myRadians * (180/PI)





To convert from degrees to radians: float radians = myDegrees * (PI/180)





Hopefully that was actually clear, I have been up for 40 hours straight, so I apologize if it's fuzzy! ;-)





HTH,


-J


----


Unlock the secrets of your true randomness!


http://Blog.GatsWiz.com

tropical flowers

No comments:

Post a Comment