Baseline Widely available
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)
.
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 Parameters
y
The y coordinate of the point.
x
The x coordinate of the point.
The angle in radians (between -Ï and Ï, inclusive) between the positive x-axis and the ray from (0, 0) to the point (x, y).
DescriptionThe 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.
Math.atan2()
is passed separate x
and y
arguments, while 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 - Ï 2 -\frac{\pi}{2} or greater than Ï 2 \frac{\pi}{2} .
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).
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)
.
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:
| 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 Browser compatibility See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4