Math.atan2()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
Math.atan2() éæ
æ¹æ³è¿åå¹³é¢ä¸ç± (0, 0) å°é» (x, y) çå°ç·èæ£ x 軸ä¹éçè§åº¦ï¼ä»¥å¼§åº¦è¡¨ç¤ºï¼ï¼å³ Math.atan2(y, x)ã
å試ä¸ä¸
function calcAngleDegrees(x, y) {
return (Math.atan2(y, x) * 180) / Math.PI;
}
console.log(calcAngleDegrees(5, 5));
// é æè¼¸åºï¼45
console.log(calcAngleDegrees(10, 10));
// é æè¼¸åºï¼45
console.log(calcAngleDegrees(0, 10));
// é æè¼¸åºï¼90
èªæ³
Math.atan2(y, x)
忏
è¿åå¼
ç± (0, 0) å°é» (x, y) çå°ç·èæ£ x 軸ä¹éçè§åº¦ï¼å¼§åº¦ï¼ï¼ç¯åå¨ -Ï å° Ïï¼å å«éçï¼ã
æè¿°
Math.atan2() æ¹æ³æ¸¬é徿£ x 軸å°é» (x, y) çéæéè§åº¦ θï¼å®ä½æ¯å¼§åº¦ãè«æ³¨æï¼æ¤æ¹æ³ç忏é åºçº y 座æ¨å
æ¼ x 座æ¨ã

Math.atan2() æ¥æ¶åå¥ç x å y 忏ï¼è Math.atan() 忥æ¶å
©ååæ¸çæ¯å¼ãMath.atan2(y, x) è Math.atan(y / x) ä¹éçå·®ç°å¦ä¸ï¼
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 (å
å« -0) |
0 | Ï | 0 |
< 0 (å
å« -0) |
-0 | -Ï | 0 |
-Infinity |
> 0 | Ï | -0 |
| -0 | > 0 | Ï / 2 | -Ï / 2 |
-Infinity |
< 0 | -Ï | 0 |
| -0 | < 0 | -Ï / 2 | Ï / 2 |
æ¤å¤ï¼å°æ¼ç¬¬äºå第ä¸è±¡éä¸çé»ï¼x < 0ï¼ï¼Math.atan2() 輸åºçè§åº¦æå°æ¼ æå¤§æ¼ ã
ç±æ¼ atan2() æ¯ Math çéæ
æ¹æ³ï¼ä½ å¿
é ä½¿ç¨ Math.atan2() ä¾å¼å«å®ï¼è䏿¯å¼å«ä½ æå»ºç«ç Math ç©ä»¶çæ¹æ³ï¼Math 䏿¯å»ºæ§åï¼ã
ç¯ä¾
>ä½¿ç¨ Math.atan2()
Math.atan2(90, 15); // 1.4056476493802699
Math.atan2(15, 90); // 0.16514867741462683
Math.atan2(y, x) å Math.atan(y / x) çå·®å¥
以ä¸è
³æ¬æè¼¸åºææ Math.atan2(y, x) å 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} |`);
}
}
}
輸åºçºï¼
| 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 |
è¦ç¯
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-math.atan2> |