computing, atan2 is a two-argument function that makes it easy to find the angle round the origin of a point. When given the coordinates ("x", "y") the angle is atan2("y", "x"). It is implemented in many different programming languages. The Linux Programmer's Manual [http://linux.ctyme.com/man/man0168.htm] says::"The atan2 C++function calculates the arctangentof the two variables "y" and "x". It is similar to calculating the arctangent of "y"/"x", except that the signs of both arguments are used to determine the quadrant of the result."
In mathematical terms atan2 computes the
principal valueof the argument function applied to the complex number"x"+i"y".That is atan2("y", "x") = Pr arg("x"+i"y") = Arg("x"+i"y"). The argument can be changed by 2π corresponding to a complete turn round the origin without making any difference to the angle, the principal value is the value in the range (-π,π] , that is -π < atan2("y", "x") ≤ π.
Effectively, this means that atan2 finds the counterclockwise
anglein radians between the "x"-axis and the vector in 2-dimensional Euclidean space, which is useful in many applications involving vectors, such as finding the direction from one point to another. A principal use is in computer graphics rotations, for converting rotation matrixrepresentations into Euler angles.
The function atan2 dates back at least as far as the
FORTRANprogramming language and is currently found in the C programming language's math.hstandard library, the Java (programming language)Math library and elsewhere. Many scripting languages, such as Perl, include the C-style atan2 function.
In other languages there are a number of variations in the name and sometimes the order of the parameters is reversed.
Some scientific calculators implement the function, it is the angle given when ("x", "y") is converted from
rectangular coordinatesto polar coordinates.
The one-argument arctangent function does not distinguish between diametrically opposite directions. For example, the anticlockwise angle from the "x"-axis to the vector <1, 1>, calculated in the usual way as arctan(1/1), is π/4 (radians), or 45°. However, the angle between the "x"-axis and the vector <−1,−1> appears, by the same method, to be arctan(−1/−1), again π/4, even though the answer clearly should be −3π/4, or −135°.
The "atan2" function takes into account the signs of both vector components, and places the angle in the correct quadrant. Thus, atan2(1, 1) = π/4 and atan2(−1, −1) = −3π/4.
Additionally, the ordinary arctangent method breaks down when required to produce an angle of ±π/2 (or ±90°). For example, an attempt to find the angle between the "x"-axis and the vector <0,1> requires evaluation of arctan(1/0), which fails on division by zero. In contrast, atan2(1,0) gives the correct answer of π/2.
When calculations are performed manually, the necessary quadrant corrections and exception handling can be done by inspection, but in computer programs it is extremely useful to have a single function that always gives an unambiguous correct result.
where is the angle in [0,π/2) such that and sgn is the
* This produces results in the range (-π,π] , which can be mapped to [0,2π) by adding 2π to negative values.
* Traditionally, atan2(0,0) is undefined. Most current implementations define it as 0. It should always return a value rather than for instance a
NaN(Not a Number).
Common Lisp, where optional arguments exist, the atan function allows to optionally supply the "x" coordinate as in (atan "x" "y")
Mathematica, the form ArcTan [x, y] is used where the one parameter form supplies the normal arctangent.
Microsoft Excel, the atan2 function has the two arguments reversed. [cite web|url=http://msdn2.microsoft.com/en-us/library/bb238940.aspx|title=Atan2 Method|publisher=Microsoft] OpenOffice.org Calcalso reverses the arguments.
* In the
IntelArchitecture assembler code, "atan2" is known as FPATAN (floating-point partial arctangent) instruction. [IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.] It can deal with infinities and results range from (closed interval), e.g. . Particularly, FPATAN is defined when both arguments are zero:
*:This definition is related with the concept of
signed zero, i.e.
* On the
TI-89calculator, the equivalent function is called R►Pθ and has the arguments reversed.
The first diagram shows values of atan2 at selected points on the unit circle. The values are shown in blue inside the circle. The four points (1,0), (0,1), (-1,0), and (0,-1) are labeled outside the circle.
The second diagram shows the values of atan2 over a region of the plane. The discontinuity along the negative half of the "x" axis is visible as a "wall" extending towards the top left of the graph.
Inverse trigonometric functionincluding atan
Complex argument (continued fraction)
* [http://www.cplusplus.com/ref/cmath/atan2.html C++ Programmer's Reference]
* [http://www.mathworks.com/access/helpdesk/help/techdoc/ref/atan2.html MATLAB Function Reference]
* [http://reference.wolfram.com/mathematica/ref/ArcTan.html Mathematica Function Reference] (note argument order is reversed)
* [http://everything2.com/index.pl?node_id=1008481 atan2] at Everything2
Other implementations/code for atan2
* [http://mathforum.org/library/drmath/view/55417.html Bearing Between Two Points]
* [http://mathforum.org/library/drmath/view/54114.html Arctan and Polar Coordinates]
* [http://mathforum.org/library/drmath/view/54172.html What's 'Arccos'?]
Wikimedia Foundation. 2010.