Articulo de referencia

atan2

atan2( y , x ) returns the angle θ between the positive x -axis and the ray from the origin to the point ( x , y ) , confined to (− π , π ] . Graph of atan2 ⁡ ( y , x ) {\displa...

atan2(y, x) returns the angle θ between the positive x-axis and the ray from the origin to the point (x, y), confined to (−π, π].
Graph of atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} over y / x {\displaystyle y/x}

In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, θ = atan2 ( y , x ) {\displaystyle \theta =\operatorname {atan2} (y,x)} is the angle measure (in radians, with π < θ π {\displaystyle -\pi <\theta \leq \pi } ) between the positive x {\displaystyle x} -axis and the ray from the origin to the point ( x , y ) {\displaystyle (x,\,y)} in the Cartesian plane. Equivalently, atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} is the argument (also called phase or angle) of the complex number x + i y . {\displaystyle x+iy.} (The argument of a function and the argument of a complex number, each mentioned above, should not be confused.)

The atan2 {\displaystyle \operatorname {atan2} } function first appeared in the programming language Fortran in 1961. It was originally intended to return a correct and unambiguous value for the angle θ {\displaystyle \theta } in converting from Cartesian coordinates ( x , y ) {\displaystyle (x,\,y)} to polar coordinates ( r , θ ) {\displaystyle (r,\,\theta )} . If θ = atan2 ( y , x ) {\displaystyle \theta =\operatorname {atan2} (y,x)} and r = x 2 + y 2 {\textstyle r={\sqrt {x^{2}+y^{2}}}} , then x = r cos θ {\displaystyle x=r\cos \theta } and y = r sin θ . {\displaystyle y=r\sin \theta .}

If x > 0 {\displaystyle x>0} , the desired angle measure is θ = atan2 ( y , x ) = arctan ( y / x ) . {\textstyle \theta =\operatorname {atan2} (y,x)=\arctan \left(y/x\right).} However, when x < 0, the angle arctan ( y / x ) {\displaystyle \arctan(y/x)} is diametrically opposite the desired angle, and ± π {\displaystyle \pm \pi } (a half turn) must be added to place the point in the correct quadrant.[1] Using the atan2 {\displaystyle \operatorname {atan2} } function does away with this correction, simplifying code and mathematical formulas.

Motivation

Graph of the tangent function from −π to with the corresponding signs of y/x. The green arrows point to the results of atan2(−1, −1) and atan2(1, 1).

The ordinary single-argument arctangent function only returns angle measures in the interval ( 1 2 π , 1 2 π ) {\displaystyle {\bigl (}{-{\tfrac {1}{2}}\pi },{\tfrac {1}{2}}\pi )} , and when invoking it to find the direction angle from the origin to an arbitrary point ( x , y ) {\displaystyle (x,\,y)} in the Cartesian plane, it will yield incorrect results when the point is in the left half-plane x < 0 {\displaystyle x<0} . Diametrically opposite angle measures have the same tangent because y / x = ( y ) / ( x ) . {\displaystyle y/x=(-y)/(-x).}

To fully determine the direction angle from the origin given a point ( x , y ) {\displaystyle (x,\,y)} using the arctangent function, mathematical formulas or computer code must handle multiple cases; at least one for positive values of x {\displaystyle x} and one for negative values of x {\displaystyle x} , and sometimes additional cases when y {\displaystyle y} is negative or one coordinate is zero. Finding angle measures and converting Cartesian to polar coordinates are common in scientific computing, and this code is redundant and error-prone.

To save programmers the trouble, computer programming languages introduced the atan2 function, at least as early as the Fortran IV language of the 1960s.[2] The quantity atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} is the angle measure between the x {\displaystyle x} -axis and a ray from the origin to a point ( x , y ) {\displaystyle (x,\,y)} anywhere in the Cartesian plane. The signs of x and y {\displaystyle y} are used to determine the quadrant of the result and select the correct branch of the multivalued function Arctan ( y / x ) {\displaystyle \operatorname {Arctan} (y/x)} .

The atan2 function is useful in many applications involving Euclidean vectors such as finding the direction from one point to another or converting a rotation matrix to Euler angles.

The atan2 function is now included in many other programming languages, and is also commonly found in mathematical formulas throughout science and engineering.

Argument order

In 1961, Fortran introduced the atan2 function with argument order ( y , x ) {\displaystyle (y,x)}