OK firstly apologies as I know this kind of question has been asked before more than once. However even after looking at the other questions and answers I have been unable to get this to work for my situation. See below for an example:
All I am simply trying to is work out the angle between P1 and P2 assuming that 0 degrees is as shown above so that I can point an arrow between the 2 in the correct direction. So I do something like this...
Point p1 = new Point(200,300); Point p2 = new Point(300,200);
double difX = p2.x - p1.x; double difY = p2.y - p1.y;
double rotAng = Math.toDegrees(Math.atan2(difY,difX));
Which comes out as: -45, where it should be 45? However it is not simply a case I don't think of it returning a negative result, as for example if I changed P1 to 300,300 (below P2) then the angle should be 0, but is returned as -90.
So I am just wondering if anyone can point out what I am doing wrong to calculate this, or is it even possible to do it this way?
O
andP2
that are closer together. The principal idea to employatan2
is good. – Chagres