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!
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
}