JavaScript(JS) math method - atan2
The atan2
method in JavaScript's Math
object returns the arctangent of the quotient of its two arguments. Unlike Math.atan
, Math.atan2
takes two arguments and returns the angle in radians between the positive x-axis and the point (x, y) in the Cartesian plane.
The atan2
function is defined as follows:
atan2(y, x) = arctan(y/x)
Note that atan2
takes the y
coordinate as the first argument, and the x
coordinate as the second argument. This is different from Math.atan
, which takes a single argument.
The atan2
method takes two arguments: y
and x
, representing the y
and x
coordinates of the point, respectively. If either argument is not a number, it will be converted to one before the calculation. If x
is zero, the result will be Math.PI / 2
if y
is positive, -Math.PI / 2
if y
is negative, and NaN
if y
is zero.
Here's an example usage:
const x = 1; const y = 1; const atan2XY = Math.atan2(y, x); console.log(atan2XY); // 0.7853981633974483
In the above example, the arctangent of the quotient of 1 and 1 is calculated using the Math.atan2
method and assigned to the atan2XY
variable. The result is then logged to the console. Note that the result is in radians. If you want the result in degrees, you can convert it by multiplying it by 180 / Math.PI
.