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)์ ๋ํด ์์ x์ถ๊ณผ (0, 0)์์ (x, y)์ ๊น์ง์ ๊ด์ ์ฌ์ด์ ํ๋ฉด ๊ฐ๋(๋ผ๋์ ๋จ์)๋ฅผ ๋ฐํํฉ๋๋ค.
์๋ํด ๋ณด๊ธฐ
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
๊ตฌ๋ฌธ
Math.atan2(y, x)
๋งค๊ฐ๋ณ์
๋ฐํ ๊ฐ
์์ x์ถ๊ณผ (0, 0)์์ (x, y) ์ง์ ๊น์ง์ ๊ด์ ์ฌ์ด์ ๊ฐ๋(-ฯ์ ฯ ์ฌ์ด, ํฌํจ)๋ฅผ ๋ผ๋์ ๋จ์๋ก ํ์ํฉ๋๋ค.
์ค๋ช
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 (including -0) |
0 | ฯ | 0 |
< 0 (including -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ยฎ 2026 Languageย Specification> # sec-math.atan2> |