File size: 5,766 Bytes
780c9fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: Math.atan2()
short-title: atan2()
slug: Web/JavaScript/Reference/Global_Objects/Math/atan2
page-type: javascript-static-method
browser-compat: javascript.builtins.Math.atan2
sidebar: jsref
---

The **`Math.atan2()`** static method returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y), for `Math.atan2(y, x)`.

{{InteractiveExample("JavaScript Demo: Math.atan2()")}}

```js interactive-example
function calcAngleDegrees(x, y) {
  return (Math.atan2(y, x) * 180) / Math.PI;
}

console.log(calcAngleDegrees(5, 5));
// Expected output: 45

console.log(calcAngleDegrees(10, 10));
// Expected output: 45

console.log(calcAngleDegrees(0, 10));
// Expected output: 90
```

## Syntax

```js-nolint
Math.atan2(y, x)
```

### Parameters

- `y`
  - : The y coordinate of the point.
- `x`
  - : The x coordinate of the point.

### Return value

The angle in radians (between -Ο€ and Ο€, inclusive) between the positive x-axis and the ray from (0, 0) to the point (x, y).

## Description

The `Math.atan2()` method measures the counterclockwise angle ΞΈ, 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.

![A diagram showing the angle returned by atan2(y, x)](atan2.png)

`Math.atan2()` is passed separate `x` and `y` arguments, while [`Math.atan()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan) is passed the ratio of those two arguments. `Math.atan2(y, x)` differs from `Math.atan(y / x)` in the following cases:

| `x`                  | `y`         | `Math.atan2(y, x)` | `Math.atan(y / x)` |
| -------------------- | ----------- | ------------------ | ------------------ |
| `Infinity`           | `Infinity`  | Ο€ / 4              | `NaN`              |
| `Infinity`           | `-Infinity` | -Ο€ / 4             | `NaN`              |
| `-Infinity`          | `Infinity`  | 3Ο€ / 4             | `NaN`              |
| `-Infinity`          | `-Infinity` | -3Ο€ / 4            | `NaN`              |
| 0                    | 0           | 0                  | `NaN`              |
| 0                    | -0          | -0                 | `NaN`              |
| < 0 (including `-0`) | 0           | Ο€                  | 0                  |
| < 0 (including `-0`) | -0          | -Ο€                 | 0                  |
| `-Infinity`          | > 0         | Ο€                  | -0                 |
| -0                   | > 0         | Ο€ / 2              | -Ο€ / 2             |
| `-Infinity`          | < 0         | -Ο€                 | 0                  |
| -0                   | < 0         | -Ο€ / 2             | Ο€ / 2              |

In addition, for points in the second and third quadrants (`x < 0`), `Math.atan2()` would output an angle less than <math><semantics><mrow><mo>-</mo><mfrac><mi>Ο€</mi><mn>2</mn></mfrac></mrow><annotation encoding="TeX">-\frac{\pi}{2}</annotation></semantics></math> or greater than <math><semantics><mfrac><mi>Ο€</mi><mn>2</mn></mfrac><annotation encoding="TeX">\frac{\pi}{2}</annotation></semantics></math>.

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 (`Math` is not a constructor).

## Examples

### Using Math.atan2()

```js
Math.atan2(90, 15); // 1.4056476493802699
Math.atan2(15, 90); // 0.16514867741462683
```

### Difference between Math.atan2(y, x) and Math.atan(y / x)

The following script prints all inputs that produce a difference between `Math.atan2(y, x)` and `Math.atan(y / x)`.

```js
const formattedNumbers = new Map([
  [-Math.PI, "-Ο€"],
  [(-3 * Math.PI) / 4, "-3Ο€/4"],
  [-Math.PI / 2, "-Ο€/2"],
  [-Math.PI / 4, "-Ο€/4"],
  [Math.PI / 4, "Ο€/4"],
  [Math.PI / 2, "Ο€/2"],
  [(3 * Math.PI) / 4, "3Ο€/4"],
  [Math.PI, "Ο€"],
  [-Infinity, "-∞"],
  [Infinity, "∞"],
]);

function format(template, ...args) {
  return String.raw(
    { raw: template },
    ...args.map((num) =>
      (Object.is(num, -0)
        ? "-0"
        : (formattedNumbers.get(num) ?? String(num))
      ).padEnd(5),
    ),
  );
}

console.log(`| x     | y     | atan2 | atan  |
|-------|-------|-------|-------|`);

for (const x of [-Infinity, -1, -0, 0, 1, Infinity]) {
  for (const y of [-Infinity, -1, -0, 0, 1, Infinity]) {
    const atan2 = Math.atan2(y, x);
    const atan = Math.atan(y / x);
    if (!Object.is(atan2, atan)) {
      console.log(format`| ${x} | ${y} | ${atan2} | ${atan} |`);
    }
  }
}
```

The output is:

```plain
| x     | y     | atan2 | atan  |
|-------|-------|-------|-------|
| -∞    | -∞    | -3Ο€/4 | NaN   |
| -∞    | -1    | -Ο€    | 0     |
| -∞    | -0    | -Ο€    | 0     |
| -∞    | 0     | Ο€     | -0    |
| -∞    | 1     | Ο€     | -0    |
| -∞    | ∞     | 3Ο€/4  | NaN   |
| -1    | -∞    | -Ο€/2  | Ο€/2   |
| -1    | -1    | -3Ο€/4 | Ο€/4   |
| -1    | -0    | -Ο€    | 0     |
| -1    | 0     | Ο€     | -0    |
| -1    | 1     | 3Ο€/4  | -Ο€/4  |
| -1    | ∞     | Ο€/2   | -Ο€/2  |
| -0    | -∞    | -Ο€/2  | Ο€/2   |
| -0    | -1    | -Ο€/2  | Ο€/2   |
| -0    | -0    | -Ο€    | NaN   |
| -0    | 0     | Ο€     | NaN   |
| -0    | 1     | Ο€/2   | -Ο€/2  |
| -0    | ∞     | Ο€/2   | -Ο€/2  |
| 0     | -0    | -0    | NaN   |
| 0     | 0     | 0     | NaN   |
| ∞     | -∞    | -Ο€/4  | NaN   |
| ∞     | ∞     | Ο€/4   | NaN   |
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{jsxref("Math.acos()")}}
- {{jsxref("Math.asin()")}}
- {{jsxref("Math.atan()")}}
- {{jsxref("Math.cos()")}}
- {{jsxref("Math.sin()")}}
- {{jsxref("Math.tan()")}}
- CSS {{cssxref("atan2()")}} function