5

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:

custom atanfull

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • 10
    This question appears to be off-topic because it is about analytical geometry – talonmies May 01 '14 at 13:24
  • 3
    I don't have an opinion on whether this is off-topic, but I usually find the Math exchange folks to be pretty accommodating towards the asker's skill level. If you ask them to, I think they'll explain it in simple terms (or at least provide links explaining the exotic symbols they're using). – Kevin May 01 '14 at 13:33
  • Here's a tip: (1) the quadrant that the point falls in can be determined by comparing the point's horizontal and vertical position to `0,0`; (2) we know that each quadrant represents 90 degrees; (3) [basic trigonometry](http://www.mathsisfun.com/sine-cosine-tangent.html) can be used to calculate the angle b/w the point and the axis (vertical or horizontal) of the circle that falls counterclockwise from the point; (4) add zero or more lots of 90 degrees (according to how many full quadrants occur before the point) to the angle calculated in (3). – jbaums May 03 '14 at 01:04
  • 1
    http://stackoverflow.com/a/1211243/13295 – Lance Roberts May 03 '14 at 01:16
  • 2
    Possible duplicate: *[Direct way of computing clockwise angle between 2 vectors](https://stackoverflow.com/questions/14066933/)* – Peter Mortensen Apr 17 '23 at 09:41
  • 1
    Related: *[Finding Signed Angle Between Vectors](https://stackoverflow.com/questions/2150050/finding-signed-angle-between-vectors)* – Peter Mortensen Apr 17 '23 at 09:43
  • 2
    Please show us your code so far. – Rohit Gupta Apr 17 '23 at 13:09
  • This is the subject of [a meta question](https://meta.stackoverflow.com/questions/424212/). – Peter Mortensen Apr 25 '23 at 14:47

2 Answers2

27

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;
}
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Just wanted to note here that in ArcGIS I had to add a check when comparing the FromPoint and ToPoint on a line to get it's full 360 degree angle I added this: if (startX > endX) { angle += 180; if (angle > 360) { angle -= 360; } } – Josh P Jul 20 '15 at 18:46
  • @Clonkex This -->http://www.cplusplus.com/reference/cmath/atan2/ website says that "If both arguments are zero, a domain error occurs". Does this problem also arise for any other combination of y and x? – Shubham Rana Jan 07 '17 at 18:15
1

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.)

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51