AppDividend
Latest Code Tutorials

JavaScript Math atan2() Function: Complete Guide

Math.atan2(y,x) function returns the angle between a point (x,y) and the positive x-axis, measured in an anticlockwise direction (in radians). The atan2() method comes in handy in programming contexts dealing with trigonometric expressions.

JavaScript Math atan2()

Javascript Math atan2() is a built-in function that returns an arctangent of the quotient of its arguments. JavaScript Math atan2() method returns a numeric value between -Π and Π representing the angle theta of the (x, y) point and the positive x-axis.

To find the arctangent quotient of two passed arguments in JavaScript, use the Math.atan2() method.

See the following figure.

JavaScript Math atan2()

Since atan2() is a static method of Math, it can be used without creating an object.

Syntax

Math.atan2(y,x)

Parameter(s)

The variables y and x, whose arctangent quotient is to be determined.

Return Value

The arctangent quotient is in the range from -pi to pi radians.

See the following figure.

 

JavaScript Math atan2() Function Example

Note:

  • If the passed value is empty, the method returns NaN.
  • If the second parameter is passed as negative zero, and the first argument is ±0, the method returns pi with the same sign as the first argument.
  • If the second parameter is passed as positive zero, and the first argument is ±0, the method returns 0 with the same sign as the first argument.
  • If the first parameter is passed as ±0, and the second argument is negative, the method returns pi with the same sign as the first argument.
  • If the first parameter is passed as ±0, and the second argument is positive, the method returns 0 with a same sign as the first argument.
  • If the second parameter is passed as ±0, and the first argument is negative, the method returns -pi/2.
  • If the second parameter is passed as ±0, and the first argument is positive, the method returns pi/2.
  • If the second parameter is passed as negative infinity, the method returns pi with the same sign as the first argument. 
  • If the second parameter is passed as positive infinity, the method returns 0 with the same sign as the first argument. 
  • If the first parameter is passed as infinite, then for the second argument is negative infinity, the method returns 3*pi/4 with the same sign as the first argument, and for the second argument is positive infinity, the method returns pi/4 with the same sign as the first argument.

Compatibility

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Android webview
  • Chrome for Android
  • Firefox for Android
  • Opera for Android
  • Safari on iOS
  • Samsung Internet
  • Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

example1.js

The following example demonstrates the use of the Javascript atan2() method.

//example1.js

var x = 5;
var y = 4;

console.log(Math.atan2(y, x));

var x = 90;
var y = 15;

console.log(Math.atan2(y, x));

var x = 15;
var y = 90;

console.log(Math.atan2(y, x));

Output

node example1
0.6747409422235527
0.16514867741462683
1.4056476493802699

example2.js

The following example demonstrates the case where an empty value is passed.

//example2.js

var x;
var y;

console.log(Math.atan2(x, y));
console.log(Math.atan2());

Output

node example2
NaN
NaN

example3.js

The following example demonstrates the following case:

If the second parameter is passed as negative zero, and the first argument is ±0, the method returns pi with the same sign as the first argument.

//example3.js

var x = -0;
var a = 0;
var b = -0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example3
3.141592653589793
-3.141592653589793

example4.js

The following example demonstrates the case: 

If the second parameter is passed as positive zero, and the first argument is ±0, the method returns 0 with the same sign as the first argument.

See the following code.

// example4.js

var x = 0;
var a = 0;
var b = -0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example4
0
-0

example5.js

The following example demonstrates the case:

 If the first parameter is passed as ±0, and the second argument is negative, the method returns pi with the same sign as the first argument.

//example5.js

var x = -5;
var a = 0;
var b = -0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example5
3.141592653589793
-3.141592653589793

example6.js

The following example demonstrates the case:

If the first parameter is passed as ±0, and the second argument is positive, the method returns 0 with the same sign as the first argument.

//example6.js

var x = 5;
var a = 0;
var b =- 0;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example6
0
-0

example7.js

The following example demonstrates the case.

 If the second parameter is passed as ±0, and the first argument is negative, the method returns -pi/2.

//example7.js

var y =- 5;
var a = 0;
var b =- 0;

console.log(Math.atan2(y,a));
console.log(Math.atan2(y,b));

Output

node example7
-1.5707963267948966
-1.5707963267948966

example8.js

The following example demonstrates the case:

 If the second parameter is passed as ±0, and the first argument is positive, the method returns pi/2.

// example8.js

var y = 5;
var a = 0;
var b =- 0;


console.log(Math.atan2(y, a));
console.log(Math.atan2(y, b));

Output

node example8
1.5707963267948966
1.5707963267948966

example9.js

The following example demonstrates the case:

If the second parameter is passed as negative infinity, the method returns pi with the same sign as the first argument.

//example9.js

var x = Number.NEGATIVE_INFINITY;
var a = 2;
var b =- 2;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example9
3.141592653589793
-3.141592653589793

example10.js

The following example demonstrates the case:

 If the second parameter is passed as negative infinity, the method returns pi with the same sign as the first argument.

//example10.js

var x = Number.POSITIVE_INFINITY;
var a = 2;
var b = -2;

console.log(Math.atan2(a, x));
console.log(Math.atan2(b, x));

Output

node example10
0
-0

example11.js

The following example demonstrates the case:

If the first parameter is passed as infinite, then for the second argument is negative infinity, the method returns 3*pi/4 with the same sign as the first argument, and for the second argument is positive infinity, the method returns pi/4 with the same sign as the first argument.

// example11.js

var x = Number.POSITIVE_INFINITY;
var y = Number.NEGATIVE_INFINITY;

console.log(Math.atan2(x, y));
console.log(Math.atan2(y, y));

console.log(Math.atan2(x, x));
console.log(Math.atan2(y, x));

Output

node example11
2.356194490192345
-2.356194490192345
0.7853981633974483
-0.7853981633974483

example12.js

The following example demonstrates the application of this method in a simple programming context.

Given the x-coordinate and y-coordinate of a point (x,y), find the angle it makes with the positive x-axis.

// example12.js

// Given the x-coordinate and y-coordinate of a point (x,y),
// find the angle it makes with the positive x-axis.

var x;
var y;

const r = require('readline');
const rl = r.createInterface({
  input: process.stdin,
  output: process.stdout
});

const prompt1 = () => {
  return new Promise((resolve, reject) => {
    rl.question('x-coordinate: ', (answer) => {
      x = answer;
      resolve();
    });
  });
};

const prompt2 = () => {
  return new Promise((resolve, reject) => {
    rl.question('y-coordinate: ', (answer) => {
      y = answer;
      resolve();
    });
  });
};

const main = async () => {
  await prompt1();
  await prompt2();
  rl.close();

  console.log("The angle (in radians) made by point (x,y) with the positive x-axis is:");
  console.log(Math.atan2(y, x));
}

main();

Output

Test Case 1:
->node example12
x-coordinate: 5
y-coordinate: 4
The angle (in radians) made by point (x,y) with the positive x-axis is:
0.6747409422235527

Test Case2:
->node example12
x-coordinate: 0
y-coordinate: 0
The angle (in radians) made by point (x,y) with the positive x-axis is:
0

See also

Javascript math cos()

Javascript math.cosh()

Javascript Math.abs()

Javascript Math.round()

Javascript Math.random()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.