Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ‚̧ ūüíĽ
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

JavaScript - Math.atan2() method example

0 contributions
6 points

The Math.atan2() function returns the angle in radians in the range -Math.PI/2 to +Math.PI/2 between the positive x-axis and the ray to the point (x, y) ‚Ȇ (0, 0).

// ONLINE-RUNNER:browser;

//                       y   x         angle in radians
console.log( Math.atan2( 2,  4) ); //  0.4636476090008061 <-   ~26.6 degrees
console.log( Math.atan2( 4, -2) ); //  2.0344439357957027 <-  ~116.6 degrees
console.log( Math.atan2(-2, -4) ); // -2.6779450445889870 <- ~-153.4 degrees
console.log( Math.atan2(-4,  2) ); // -1.1071487177940904 <-  ~-63.4 degrees

atan2() method has been visualised on below image:

atan2(y, x) function visualization - JavaScript Math Object.
atan2(y, x) function visualization - JavaScript Math Object.

1. Documentation

SyntaxMath.atan2(y, x)
Parametersy, x - integer or float number values that are coords of point (primitive value).
Result

number value of angle between two lines OP and OX in radians in the range -Math.PI/2 to +Math.PI/2 (primitive value).

Where:

  • O=(0, 0)¬†- intersection point of coordinate system axes,
  • P=(x, y)¬†- out point,
  • OX - positive part of x axis.

If point in in 1st (I) or 2nd (II) quadrant angle is measured in counterclockwise direction.

If point in in 3rd (III) or 4th (IV) quadrant angle is measured in clockwise direction.

Note: angle arrows show measured angles for points located in different quadrants.

Description

atan2 is a static method that takes two parameters and returns an approximation of the arctangent(y/x) function taking at account quadrants of point location (P=(x, y)).


2. Working with degrees

// ONLINE-RUNNER:browser;

function calculateAngle(y, x) {
	var angle = Math.atan2(y, x);

    return (180 / Math.PI) * angle; // rad to deg conversion
}

// Example usage:

console.log( calculateAngle( 2,  4) ); //   26.56505117707799 degrees
console.log( calculateAngle( 4, -2) ); //  116.56505117707799 degrees
console.log( calculateAngle(-2, -4) ); // -153.43494882292200 degrees
console.log( calculateAngle(-4,  2) ); //  -63.43494882292201 degrees

3.  Conversion to only clockwise angles in degrees

This section shows how to convert angles to clockwise angles (from 0 to 360 degrees).

// ONLINE-RUNNER:browser;

function calculateAngle(y, x) {
	var angle = Math.atan2(y, x);
  
  	if (angle < 0.0) {
    	angle += 2.0 * Math.PI;
    }

    return (180 / Math.PI) * angle; // rad to deg conversion
}

// Example usage:

console.log( calculateAngle( 2,  4) ); //  26.56505117707799 degrees
console.log( calculateAngle( 4, -2) ); // 116.56505117707799 degrees
console.log( calculateAngle(-2, -4) ); // 206.56505117707800 degrees
console.log( calculateAngle(-4,  2) ); // 296.56505117707800 degrees

4.  Conversion to only counterclockwise angles in degrees

This section shows how to convert angles to counterclockwise angles (from -360 to 0 degrees).

// ONLINE-RUNNER:browser;

function calculateAngle(y, x) {
	var angle = Math.atan2(y, x);
  
  	if (angle > 0.0) {
    	angle -= 2.0 * Math.PI;
    }

    return (180 / Math.PI) * angle; // rad to deg conversion
}

// Example usage:

console.log( calculateAngle( 2,  4) ); // -333.434948822922 degrees
console.log( calculateAngle( 4, -2) ); // -243.434948822922 degrees
console.log( calculateAngle(-2, -4) ); // -153.434948822922 degrees
console.log( calculateAngle(-4,  2) ); // -63.4349488229220 degrees

References

  1. atan2 - Wikipedia
  2. Inverse trigonometric functions - Wikipedia
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more