Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript atan2() method

JavaScript atan2() method

Math Object Reference JavaScript Math object

Example

Example 1 - atan2(y,x)

Given a coordinate (x, y) with the coordinate values (4, 8), use the atan2() method to calculate the angle in radians between the coordinate and the X axis, as in the following example:

Math.atan2(8,4);

Output of the above example:

1.1071487177940904

Try it »


Definition and Usage

atan2() returns the plane angle (in radians) between the line segment from the origin (0,0) to the point (x,y) and the positive direction of the x axis, that is, Math.atan2(y,x).

Note: atan2() has two parameters: the y coordinate is passed as the first parameter, and the x coordinate is passed as the second parameter.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the atan2() method.


Syntax

Math.atan2(

y

,

x

)

Parameters Description
y Required. A number representing the Y coordinate
x Required. A number representing the x coordinate

Return Value

Type Description
Number The arctangent of x. Returns a value between -PI and PI. It represents the offset angle corresponding to the point (x, y). This is a counterclockwise angle in radians, between the positive X axis and the line connecting the point (x, y) to the origin. Note the order of parameters accepted by this function: the y coordinate is passed first, then the x coordinate.

Technical Details

JavaScript version: 1.0

More examples

Example

Math

.

atan2

(

90

,

15

)

// 1.4056476493802699

Math

.

atan2

(

15

,

90

)

// 0.16514867741462683

Math

.

atan2

(

±

0

,

0

)

             

// ±PI.

Math

.

atan2

(

±

0

,

0

)

             

// ±0.

Math

.

atan2

(

±

0

,

x

)

             

// ±PI for x > 0.

Math

.

atan2

(

±

0

,

x

)

               

// ±0 for x > 0.

Math

.

atan2

(

y

,

±

0

)

             

// -PI/2 for y > 0.

Math

.

atan2

(

y

,

±

0

)

               

// PI/2 for y > 0.

Math

.

atan2

(

±y

,

Infinity

)

       

// ±PI for finite y > 0.

Math

.

atan2

(

±y

,

Infinity

)

       

// ±0 for finite y > 0.

Math

.

atan2

(

±

Infinity

,

x

)

       

// ±PI/2 for finite x.

Math

.

atan2

(

±

Infinity

,

Infinity

)

// ±3*PI/4.

Math

.

atan2

(

±

Infinity

,

Infinity

)

// ±PI/4.


Math Object Reference JavaScript Math object