Reputation: 1193
I have two points (x1,y1) and (x2,y2) in mathematical coordinates(bottom-left is origin). I have a scale factor SCALE
to scale these coordinates to my screen coordinates and then Complementing the height to get the Original Screen coordinate(top-left is origin). I want to know the angle made by the 2 points in screen coordinate system. Currently i am doing the following:
theta=Math.atan((y2-y1)/(x2-x1));
if(theta<0)
theta+=Math.PI;
But it is not working. Can i do this without scaling the coordinates?
EDIT: Sample Output for x1= 7.2 y1= 9.575 x2= 5.4 y2= 7.15
is 53.414 degree.
Upvotes: 1
Views: 131
Reputation: 2896
This equation should work for you.
float angle = (float) Math.toDegrees(Math.atan2(y_2 - y_1, x_2 - x_1));
//Take this out if you want negative values.
if(angle < 0){
angle += 360;
}
This will return a value between 0 and 359. If you want negative values just take out the if statement.
You say it doesn't work. Do you mean to tell me that my eyes are lying to me?
Based on your above comment that you are expecting 233.41467. You should leave in the angle += 360
Upvotes: 1
Reputation: 375
I think the standard equation for calculating the angle between two vectors of R^2 is the following: (assuming that alpha is the angle between the two vectors, v1 and v2 are the two vectors)
cos(alpha) = abs(dot(v1, v2)) / (norm(v1) * norm(v2))
So after scaling the two vectors v1=(x1, y1)^t and v2=(x2, y2)^t to screen coordiantes, you should use the formula above to calculate alpha. (dot means the dotproduct between v1 and v2, and norm is the standard euclidean metric).
After that you obtain the angle in radians (for a conversion to degrees you can simply divide by 2*Pi and multiply by 360).
Upvotes: 0