atan2

(PHP 4, PHP 5, PHP 7, PHP 8)

atan2Arco tangente di due variabili

Descrizione

atan2(float $y, float $x): float

Questa funzione calcola l'arco tangente delle due variabili x e y. È simile al calcolo dell'arco tangente di y / x, eccetto per il fatto che viene tenuto in considearazione il segno di entrambi gli argomenti per determinare il quadrante del risultato.

La funzione restituisce il risultato in radianti, compreso fra -PI e PI (inclusi).

Vedere anche acos() e atan().

add a note add a note

User Contributed Notes 5 notes

up
15
reubs at idsdatanet dot com
21 years ago
Just a note:

PHP's atan2 function receives parameters in (y,x) and Excel receives it in (x,y) format. Just in case you are porting formulas across. :)
up
9
fred dot beck at rrd dot com
15 years ago
<?php
/**
*    Given an origin point of (0,0) and a destination point $x,$y
*  somewhere on an axis grid, compass() determines the compass
*  heading(direction) of the destination point from the origin
*   
*  HOWEVER, atan2(y,x)'s natural compass thinks east is north,
*
*  {135}-------{ 90}-------{45}
*      | +-----[ +y]-----+ | 
*      | |               | | 
*      | |               | | 
*  {180} [-x]  [0,0]  [+x] {0} <--------- North ?
*      | |               | | 
*      | |               | | 
*      | +-----[ -y]-----+ |
* {-135}-------{-90}-------{-45}
*
*
*    SO, we simply transpose the (y,x) parameters to atan2(x,y)
*     which will both rotate(left) and reflect(mirror) the compass.
*
*  Which gives us this compass
*
*  {-45}-------{ 0 }-------{45}
*      | +-----[ +y]-----+ | 
*      | |               | | 
*      | |               | | 
*  {-90} [-x]  [0,0]  [+x] {90}
*      | |               | | 
*      | |               | | 
*      | +-----[ -y]-----+ |
* {-135}-------{180}-------{135}
*
*  FINALLY,` we check if param $x was indeed a negative number,
*  if so we simply add 360 to the negative angle returned by atan2()
*   
*/
function compass($x,$y)
    {
        if(
$x==0 AND $y==0){ return 0; } // ...or return 360
       
return ($x < 0)
        ?
rad2deg(atan2($x,$y))+360      // TRANSPOSED !! y,x params
       
: rad2deg(atan2($x,$y));
    }
function
polar($x,$y)
    {
       
$N = ($y>0)?'N':'';
       
$S = ($y<0)?'S':'';
       
$E = ($x>0)?'E':'';
       
$W = ($x<0)?'W':'';       
        return
$N.$S.$E.$W;
    }
function
show_compass($x,$y)
     {
         return
'<BR>'
            
.polar($x,$y)
             .
' compass( x='.$x.', y='.$y.' )= '
            
.number_format(compass($x,$y),3).'&deg';
     }

echo
show_compass(0,3);
echo
show_compass(.06,3);
echo
show_compass(3,3);
echo
show_compass(3,.06);
echo
show_compass(3,0);
echo
show_compass(3,-.06);
echo
show_compass(3,-3);
echo
show_compass(.06,-3);
echo
show_compass(0,-3);
echo
show_compass(-.06,-3);
echo
show_compass(-3,-3);
echo
show_compass(-3,-.06);
echo
show_compass(-3,0);
echo
show_compass(-3,.06);
echo
show_compass(-3,3);
echo
show_compass(-.06,3);

/* RENDERS THIS

N compass( x=0, y=3 )= 0 °
NE compass( x=0.06, y=3 )= 1.14576283818 °
NE compass( x=3, y=3 )= 45 °
NE compass( x=3, y=0.06 )= 88.8542371618 °
E compass( x=3, y=0 )= 90 °
SE compass( x=3, y=-0.06 )= 91.1457628382 °
SE compass( x=3, y=-3 )= 135 °
SE compass( x=0.06, y=-3 )= 178.854237162 °
S compass( x=0, y=-3 )= 180 °
SW compass( x=-0.06, y=-3 )= 181.145762838 °
SW compass( x=-3, y=-3 )= 225 °
SW compass( x=-3, y=-0.06 )= 268.854237162 °
W compass( x=-3, y=0 )= 270 °
NW compass( x=-3, y=0.06 )= 271.145762838 °
NW compass( x=-3, y=3 )= 315 °
NW compass( x=-0.06, y=3 )= 358.854237162 °

*/

?>
up
1
mirellastitan at gmail dot com
7 years ago
The russian translation for this article contains a translation error.

Делитель and Делимое should be visa versa:

Делимое is the dividend
Делитель is the divisor

Thank you in advance for fixing this issue. It's quite confusing
up
0
Monte Shaffer
17 years ago
Here is a function that will return a new point [Rotate around non-origin pivot point]

(x,y) is current point
(cx,cy) is pivot point to rotate
=a= is angle in degrees

    $_rotation     = 1;      # -1 = counter, 1 = clockwise
    $_precision    = 2;      # two decimal places

function returnRotatedPoint($x,$y,$cx,$cy,$a)
    {
    # http://mathforum.org/library/drmath/view/63184.html
    global $_rotation;     # -1 = counter, 1 = clockwise
    global $_precision;    # two decimal places

   
            // radius using distance formula
            $r = sqrt(pow(($x-$cx),2)+pow(($y-$cy),2));
            // initial angle in relation to center
            $iA = $_rotation * rad2deg(atan2(($y-$cy),($x-$cx)));

            $nx = number_format($r * cos(deg2rad($_rotation * $a + $iA)),$_precision);
            $ny = number_format($r * sin(deg2rad($_rotation * $a + $iA)),$_precision);

    return array("x"=>$cx+$nx,"y"=>$cy+$ny);
    }
up
-2
andyidol at gmail dot com
14 years ago
This function will return degree by vertex coordinates in general trigonometrical coordinate system where zero is located at position (1, 0), 90' at (0, 1), 180' at (-1, 0) and so on.

<?php

function GetDegree($x, $y)
{
 
// we don't want to cause division by zero
 
if($x == 0) $x = 1 / 10000;
     
 
$deg = rad2deg(atan(abs($y / $x)));
     
  if(
$y >= 0) $deg = $x < 0 ? 180 - $deg : $deg;
  else       
$deg = $x < 0 ? 180 + $deg : 360 - $deg;
     
  return
$deg;
     
}

?>
To Top