I was reading this post about getting an angle between 2 points and was wondering. I thought atan2 is defined for atan2(y,x) here it is atan2(deltaX, deltaY), why is x first now?
public float getAngle(Point target) {
float angle = (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y));
if (angle < 0) {
angle += 360;
}
return angle;
}
Math.java it define as
public static double atan2(double y, double x) {
return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
}
and this will return the counter-clock wise angle with respect to X- axis.
If you interchange those two you will get the clock wise angle with respect to X- axis.
In Cartesian coordinate system we consider counter-clock wise angle with respect to X-axis. That is why Math.java use this as above.
Swapping the order of the arguments means that instead of the (counter-clockwise) angle with the X-axis you get the (clockwise) angle with the Y-axis. It's not wrong, just unusual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With