Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Math:atan2

From MDC


Contents

[edit] Summary

Returns the arctangent of the quotient of its arguments.

Method of Math

Static

Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

[edit] Syntax

Math.atan2(y, x)

[edit] Parameters

y, x 
Numbers.

[edit] Description

The atan2 method returns a numeric value between -pi and pi representing the angle theta of an (x,y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x,y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.

atan2 is passed separate x and y arguments, and atan is passed the ratio of those two arguments.

Because atan2 is a static method of Math, you always use it as Math.atan2(), rather than as a method of a Math object you created.

[edit] Examples

[edit] Example: Using Math.atan2

The following function returns the angle of the polar coordinate:

function getAtan2(y,x) {
   return Math.atan2(y,x)
}

If you pass getAtan2 the values (90,15), it returns 1.4056476493802699; if you pass it the values (15,90), it returns 0.16514867741462683.

Math.atan2( ±0, -0 ) returns ±PI.
Math.atan2( ±0, +0 ) returns ±0.
Math.atan2( ±0, -x ) returns ±PI for x < 0.
Math.atan2( ±0, x ) returns ±0 for x > 0.
Math.atan2( y, ±0 ) returns -PI/2 for y > 0.
Math.atan2( ±y, -Infinity ) returns ±PI for finite y > 0.
Math.atan2( ±y, +Infinity ) returns ±0 for finite y > 0.
Math.atan2( ±Infinity, +x ) returns ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) returns ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) returns ±PI/4.

[edit] See Also

acos, asin, atan, cos, sin, tan