If you look at a circle with the angles marked you will see that 270 degrees
corresponds with (0,-1)
i.e. straight down.
By convention degrees are marked as acceding in a counter-clockwise direction. They do however form a loop and as such 270 degrees
(purple arrow) can also be expressed as -90 degrees
(orange arrow) i.e. a quarter turn in the opposite direction.
The coordinates that you provided (-2, -145)
correspond with both 269.21
and -90.79 degrees
depending on how you choose to express this angle..
The documentation for Atan2()
for .Net 4.5 states that it will return value is the range -π <= θ <= π
, after converting to degrees this is -180 <= θ <= 180
.
To further explain the expected results here is a table with the results of the following formula with the corresponding values of x
and y
.
angle = Math.Atan2(y, x) * 180 / Math.PI;
x -1 0 1
y +------------------------
1 | 135 90 45
0 | 180 0 0
-1 | -135 -90 -45
If you prefer to have your angles expressed in the range 0 <= θ <= 360
they are easy to convert:
while (angle > 360) {
angle -= 360;
}
while (angle < 0) {
angle += 360;
}
xDiff
,yDiff
andangle
when it gives the wrong result? \$\endgroup\$