Dieser Inhalt wurde automatisch aus dem Englischen รผbersetzt, und kann Fehler enthalten. Erfahre mehr รผber dieses Experiment.

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 โจJuli 2015โฉ.

Die statische Methode Math.atan2() gibt den Winkel in der Ebene (in Radiant) zwischen der positiven x-Achse und dem Strahl von (0, 0) zu dem Punkt (x, y) zurรผck, fรผr Math.atan2(y, x).

Probieren Sie es aus

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

Syntax

js
Math.atan2(y, x)

Parameter

y

Die y-Koordinate des Punktes.

x

Die x-Koordinate des Punktes.

Rรผckgabewert

Der Winkel in Radiant (zwischen -ฯ€ und ฯ€, inklusive) zwischen der positiven x-Achse und dem Strahl von (0, 0) zu dem Punkt (x, y).

Beschreibung

Die Methode Math.atan2() misst den gegen den Uhrzeigersinn gerichteten Winkel ฮธ, in Radiant, zwischen der positiven x-Achse und dem Punkt (x, y). Beachten Sie, dass die Argumente dieser Funktion zuerst die y-Koordinate und dann die x-Koordinate รผbergeben.

Ein Diagramm, das den Winkel zeigt, der von atan2(y, x) zurรผckgegeben wird

Math.atan2() wird getrennte x- und y-Argumente รผbergeben, wรคhrend Math.atan() das Verhรคltnis dieser beiden Argumente รผbergeben wird. Math.atan2(y, x) unterscheidet sich von Math.atan(y / x) in den folgenden Fรคllen:

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 (einschlieรŸlich -0) 0 ฯ€ 0
< 0 (einschlieรŸlich -0) -0 -ฯ€ 0
-Infinity > 0 ฯ€ -0
-0 > 0 ฯ€ / 2 -ฯ€ / 2
-Infinity < 0 -ฯ€ 0
-0 < 0 -ฯ€ / 2 ฯ€ / 2

Zusรคtzlich wรผrde Math.atan2() fรผr Punkte im zweiten und dritten Quadranten (x < 0) einen Winkel ausgeben, der kleiner als -ฯ€2-\frac{\pi}{2} oder grรถรŸer als ฯ€2\frac{\pi}{2} ist.

Da atan2() eine statische Methode von Math ist, verwenden Sie sie immer als Math.atan2(), anstatt als Methode eines von Ihnen erstellten Math-Objekts (Math ist kein Konstruktor).

Beispiele

Verwendung von Math.atan2()

js
Math.atan2(90, 15); // 1.4056476493802699
Math.atan2(15, 90); // 0.16514867741462683

Unterschied zwischen Math.atan2(y, x) und Math.atan(y / x)

Das folgende Skript gibt alle Eingaben aus, die eine Differenz zwischen Math.atan2(y, x) und Math.atan(y / x) erzeugen.

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

Die Ausgabe ist:

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

Spezifikationen

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-math.atan2

Browser-Kompatibilitรคt

Siehe auch