views:

554

answers:

4

In C the atan2 function has the following signature:

double atan2( double y, double x );

Other languages do this as well. This is the only function I know of that takes its arguments in Y,X order rather than X,Y order, and it screws me up regularly because when I think coordinates, I think (X,Y).

Does anyone know why atan2's argument order convention is this way?

+13  A: 

Because I believe it is related to arctan(y/x), so y appears on top.

Here's a nice link talking about it a bit: Angles and Directions

CookieOfFortune
Some programmer in the mists of time decided it made sense that way. (And why is it 'atan2'? Why not 'angle_for'? I used to think it was arctangent-squared...)
Alex Feinman
I think it makes a lot of sense...
CookieOfFortune
Yeah, arctan(y/x) happens so often that if arctan2 took x,y it would screw me up all the time. It's nice to just change the slash to a comma.
Nosredna
@alex - it's atan2 because it takes 2 arguments, to distinguish it from atan(), which expects you to do the y/x division beforehand.
JustJeff
That's not the only reason, atan() does not handle the difference when both x and y are negative, whereas atan2() does.
CookieOfFortune
+7  A: 

My assumption has always been that this is because of the trig definition, ie that

tan(theta) = opposite / adjacent

When working with the canonical angle from the origin, opposite is always Y and adjacent is always X, so:

atan2(opposite, adjacent) = theta

Ie, it was done that way so there's no ordering confusion with respect to the mathematical definition.

Not Sure
rise over run ....
larson4
Soh Cah Toa is my pnemonic :)
Not Sure
good'ol 7th grade geometry, I still use it sometimes.
CookieOfFortune
7th grade? You must not have gone to public school :)
Not Sure
+1  A: 

Suppose a rectangle triangle with its opposite side called y, adjacent side called x:

tan(angle) = y/x

arctan(tan(angle)) = arctan(y/x)

Marco Mustapic
A: 

It's because in school, the mnemonic for calculating the gradient
is rise over run, or in other words dy/dx, or more briefly y/x.

And this order has snuck into the arguments of arctangent functions.

So it's a historical artefact. For me it depends on what I'm thinking
about when I use atan2. If I'm thinking about differentials, I get it right
and if I'm thinking about coordinate pairs, I get it wrong.

Rhythmic Fistman
I wonder if it would be more clear if the variables were labeled dx and dy?
CookieOfFortune