La funció Math.atan2() retorna l'arctangent del quocient dels arguments passats.
Sintaxi
Math.atan2(y, x)
Paràmetres
y- Primer nombre.
x- Segon nombre.
Descripció
El mètode Math.atan2() retorn aun valor numèric entre -π i π que representa l'angle theta d'un punt (x, y). Aquest angle es representa en radiants, en sentit contrari a les agulles del rellotge, entre l'eix positiu X i el punt (x, y). Cal destacar que els arguments d'aquesta funció representen les coordenades del punt, on el primer argument representa la coordenada y i el segon argument representa la coordenada x.
Math.atan2() rep els arguments x i y de forma separada mentre que Math.atan() rep la relació (ratio) entre aquests dos arguments.
Com que que atan2() és un mètode estàtic de Math, sempre s'utilitza com a Math.atan2(), en comptes de com a mètode d'una instància de Math (Math no és un constructor).
Exemples
Utilitzar Math.atan2()
Math.atan2(90, 15); // 1.4056476493802699 Math.atan2(15, 90); // 0.16514867741462683 Math.atan2(±0, -0); // ±PI. Math.atan2(±0, +0); // ±0. Math.atan2(±0, -x); // ±PI for x > 0. Math.atan2(±0, x); // ±0 for x > 0. Math.atan2(-y, ±0); // -PI/2 for y > 0. Math.atan2(y, ±0); // PI/2 for y > 0. Math.atan2(±y, -Infinity); // ±PI for finite y > 0. Math.atan2(±y, +Infinity); // ±0 for finite y > 0. Math.atan2(±Infinity, x); // ±PI/2 for finite x. Math.atan2(±Infinity, -Infinity); // ±3*PI/4. Math.atan2(±Infinity, +Infinity); // ±PI/4.
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Definició inicial. Implementat a JavaScript 1.0. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Math.atan2' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.atan2' in that specification. |
Standard |
Compatibilitat amb navegadors
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |