JavaScript atan2() method

Definition and usage

atan2() The method can return the angle between the x-axis and the point (x,y).

atan2() The method returns the arctangent of the quotient of its parameters, returning a value between -PI and PI.

The returned number represents the counterclockwise angle (not in degrees) between the positive X-axis and the point (x,y) in radians.

Note:Note the order of the function's parameters, with the Y coordinate passed before the X coordinate.

Example - atan2(y,x)

Assuming you have a point (x,y) at (4,8), you can calculate the angle between the point and the positive X-axis as follows:

Math.atan2(8, 4);

Try it yourself

Syntax

Math.atan2(y, x)
Parameters Description
y Required. Number. Represents the y coordinate.
x Required. Number. Represents the x coordinate.

Technical details

Return value: Numbers, from PI to -PI, or NaN if the value is empty.
JavaScript version: ECMAScript 1

Webbläsarstöd

Math.atan2() Är ES1-funktioner (JavaScript 1999). Alla webbläsare stöder det fullt ut:

Chrome IE Edge Firefox Safari Opera
Stöd Stöd Stöd Stöd Stöd Stöd

Relaterade sidor

Tutorial:JavaScript matematik