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
Math.atan2(y, x)
Parameter
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.
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 oder grรถรer als 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()
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.
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
Loadingโฆ