PHP Conference Japan 2024

atan2

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

atan2兩個變數的反正切

說明

atan2(浮點數 $y, 浮點數 $x): 浮點數

此函式計算兩個變數 xy 的反正切。它類似於計算 y / x 的反正切,不同之處在於兩個參數的正負號都用於確定結果的象限。

此函式以弧度返回結果,範圍在 -PI 到 PI 之間(含)。

參數

y

被除數參數

x

除數參數

回傳值

y/x 的反正切,以弧度表示。

參見

新增註釋

使用者貢獻的註釋 6 則註釋

reubs at idsdatanet dot com
21 年前
只是一則註釋

PHP 的 atan2 函數接收參數的順序是 (y,x),而 Excel 接收參數的順序是 (x,y)。如果您正在移植公式,請注意這一點。 :)
fred dot beck at rrd dot com
15 年前
<?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 °

*/

?>
mirellastitan at gmail dot com
7 年前
這篇文章的俄文翻譯有一個翻譯錯誤。

Делитель 和 Делимое 應該反過來

Делимое 是被除數
Делитель 是除數

先感謝您修正這個問題。這相當令人困惑
Monte Shaffer
17 年前
以下是一個函數,它將返回一個新的點 [圍繞非原點的樞軸點旋轉]

(x,y) 是目前的點
(cx,cy) 是要旋轉的樞軸點
=a= 是角度,以度為單位

$_rotation = 1; # -1 = 逆時針,1 = 順時針
$_precision = 2; # 小數點後兩位

function returnRotatedPoint($x,$y,$cx,$cy,$a)
{
# http://mathforum.org/library/drmath/view/63184.html
global $_rotation; # -1 = 逆時針,1 = 順時針
global $_precision; # 小數點後兩位


// 使用距離公式計算半徑
$r = sqrt(pow(($x-$cx),2)+pow(($y-$cy),2));
// 相對於中心的初始角度
$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);
}
biziclop at example dot com
7 小時前
<?php
// 如果 $radians 介於 -PI … +PI 之間,則
atan2( sin( $radians ), cos( $radians )) ≈ $radians
?>
andyidol at gmail dot com
14 年前
此函數將根據一般三角坐標系中的頂點坐標返回角度,其中零位於位置 (1, 0),90 度位於 (0, 1),180 度位於 (-1, 0),依此類推。

<?php

函式 GetDegree($x, $y)
{
// 避免除以零
如果($x == 0) $x = 1 / 10000;

$deg = rad2deg(atan(abs($y / $x)));

如果
($y >= 0) $deg = $x < 0 ? 180 - $deg : $deg;
否則
$deg = $x < 0 ? 180 + $deg : 360 - $deg;

返回
$deg;

}

?>
To Top