Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. atan2()

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 July 2015.

TheMath.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), forMath.atan2(y, x).

Try it

function calcAngleDegrees(x, y) {  return (Math.atan2(y, x) * 180) / Math.PI;}console.log(calcAngleDegrees(5, 5));// Expected output: 45console.log(calcAngleDegrees(10, 10));// Expected output: 45console.log(calcAngleDegrees(0, 10));// Expected output: 90

Syntax

js
Math.atan2(y, x)

Parameters

y

The y coordinate of the point.

x

The x coordinate of the point.

Return value

The angle in radians (between -π and π, inclusive) between the positive x-axis and the ray from (0, 0) to the point (x, y).

Description

TheMath.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.

A diagram showing the angle returned by atan2(y, x)

Math.atan2() is passed separatex andy arguments, whileMath.atan() is passed the ratio of those two arguments.Math.atan2(y, x) differs fromMath.atan(y / x) in the following cases:

xyMath.atan2(y, x)Math.atan(y / x)
InfinityInfinityπ / 4NaN
Infinity-Infinity-π / 4NaN
-InfinityInfinity3π / 4NaN
-Infinity-Infinity-3π / 4NaN
000NaN
0-0-0NaN
< 0 (including-0)0π0
< 0 (including-0)-00
-Infinity> 0π-0
-0> 0π / 2-π / 2
-Infinity< 00
-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}.

Becauseatan2() is a static method ofMath, you always use it asMath.atan2(), rather than as a method of aMath object you created (Math is not a constructor).

Examples

Using Math.atan2()

js
Math.atan2(90, 15); // 1.4056476493802699Math.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 betweenMath.atan2(y, x) andMath.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} |`);    }  }}

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

Specification
ECMAScript® 2026 Language Specification
# sec-math.atan2

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp