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() ã¯éçã¡ã½ããã§ãMath.atan2(y, x) ã«å¯¾ãã¦ç¹ (0, 0) ããç¹ (x, 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)
弿°
è¿å¤
æ£ã® x 軸㨠(0, 0) ãã (x, y) ã¸ã®åç´ç·ã¨ã®éã®è§åº¦ã®ã©ã¸ã¢ã³ï¼-Ï ï½ Ï ã§ä¸¡ç«¯ãå«ãï¼ã§ãã
解説
Math.atan2() ã¡ã½ããã¯ãæ£ã® x 軸㨠(x, y) 座æ¨ã®è§åº¦ θ ãã©ã¸ã¢ã³ã§è¡¨ãè¨æ¸¬ãã¾ãããªãããã®é¢æ°ã¸ã®å¼æ°ã¯ãå
ã« y 座æ¨ã次㫠x 座æ¨ã§ãããã¨ã«æ³¨æãã¦ãã ããã

Math.atan2() ã¯ãx 㨠y ãå¥ã
ã«åãåãã®ã«å¯¾ãã Math.atan() 㯠2 ã¤ã®å¼æ°ã®æ¯çãåãåãã¾ãã 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 ãªãã¸ã§ã¯ãã®ã¡ã½ããã¨ãã¦ã§ã¯ãªãã常㫠Math.atan2() ã¨ãã¦ä½¿ç¨ããããã«ãã¦ãã ããï¼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> |