How can I find the angle (0 to 360 degrees, rotating clockwise) between 0,1 and another point (in the following diagram, 0.3,-0.17), with the origin at 0,0? Here's a somewhat crudely drawn illustration of what I need:

The circle on the left is purely for showing which direction I want the angles to rotate, and from where they start/end. The drawing on the right gives an example of the input I would supply to the code (i.e., 0.3,-0.17). The green line is resulting angle.
How do I find the angle between two points, as described above, in C++ or JavaScript?
The atan2 function gives the angle of a point with respect to the X axis, given the point's x and y coordinates. The result typically ranges betwen -180 and 180 degrees, but we can adjust that to [0, 360] later.
You can find the angle between two lines A and B that extend from the origin, by subtracting their atan results:
angle = atan2(a.y, a.x) - atan2(b.y, b.x);
Here, your A point would be (0,1) and your B point would be (0.3, -0.17).
atan2 usually returns the angle in radians and not degrees (check your language's documentation to be sure). If this is the case, you should convert it into degrees here.
angle = angle * 360 / (2*pi);
angle will now be somewhere between -360 and 360 degrees, so you'll need to perform an additional check to get it in the desired range.
if (angle < 0){
angle = angle + 360;
}
If you flip the image about the diagonal, i.e., exchange the x and y coordinates, the distribution of angles is the usual one for trigonometry. Thus
angle_in_degrees=atan2(x,y)*180/pi
plus any corrections for the range if you do not want [-180°..180°].
(Note that atan2 arguments are usually in the order (y, x), but here we are swapping them.)
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