## Getting angle between two points

Member
Posts: 370
Joined: 2002.04
Post: #1
Ok, I'm trying to have an icon face the mouse. I have the mouse location and the icon's location. I want the angle which will make the icon face the mouse location. Simple: atan(dy/dx), right? The problem is that it doesn't work unless the mouse is up and right from the icon. How do I compensate for this? Thanks.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Sage
Posts: 1,231
Joined: 2002.10
Post: #2
You want to use atan2, not atan:

DESCRIPTION
The atan() and atanf() functions compute the principal value of the arc tangent of x in the range [-pi/2, +pi/2].

versus:

DESCRIPTION
The atan2() and atan2f() functions compute the principal value of the arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value.

RETURN VALUES
The atan2 function, if successful, returns the arc tangent of y/x in the range [-pi, +pi] radians. If both x and y are zero, the global variable errno is set to EDOM. On the VAX:

atan2(y, x) := atan(y/x) if x > 0,
sign(y)*(pi - atan(|y/x|)) if x < 0,
0 if x = y = 0, or
sign(y)*pi/2 if x = 0 y.
Member
Posts: 370
Joined: 2002.04
Post: #3
Oh, so that's what that's for

Thanks a bunch.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?