This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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

๊ตฌ๋ฌธ

js
Math.atan2(y, x)

๋งค๊ฐœ๋ณ€์ˆ˜

y

์ ์˜ y์ขŒํ‘œ.

x

์ ์˜ x์ขŒํ‘œ.

๋ฐ˜ํ™˜ ๊ฐ’

์–‘์˜ x์ถ•๊ณผ (0, 0)์—์„œ (x, y) ์ง€์ ๊นŒ์ง€์˜ ๊ด‘์„  ์‚ฌ์ด์˜ ๊ฐ๋„(-ฯ€์™€ ฯ€ ์‚ฌ์ด, ํฌํ•จ)๋ฅผ ๋ผ๋””์•ˆ ๋‹จ์œ„๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์„ค๋ช…

Math.atan2() ๋ฉ”์„œ๋“œ๋Š” ์–‘์ˆ˜ x์ถ•๊ณผ ์  (x, y) ์‚ฌ์ด์˜ ์‹œ๊ณ„ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ ๊ฐ๋„ ฮธ๋ฅผ ๋ผ๋””์•ˆ ๋‹จ์œ„๋กœ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋Š” y ์ขŒํ‘œ๋ฅผ ๋จผ์ € ์ „๋‹ฌํ•˜๊ณ  x ์ขŒํ‘œ๋ฅผ ๋‘ ๋ฒˆ์งธ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

atan2(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()๋Š” -ฯ€2-\frac{\pi}{2}๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ฯ€2\frac{\pi}{2}๋ณด๋‹ค ํฐ ๊ฐ๋„๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

atan2()๋Š” Math์˜ ์ •์  ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ, ์ƒ์„ฑํ•œ Math ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•ญ์ƒ Math.atan2()๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(Math๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค).

์˜ˆ์ œ

Math.atan2() ์‚ฌ์šฉํ•˜๊ธฐ

js
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)์˜ ์ฐจ์ด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

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} |`);
    }
  }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

| 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

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ