©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 4, PHP 5)
atan2 — 两个参数的反正切
$y
, float $x
)
本函数计算两个变量
x
和 y
的反正切值。和计算
y
/ x
的反正切相似,只除了两个参数的符号是用来确定结果的象限之外。
本函数的结果为弧度,其值在 -PI 和 PI 之间(包括 -PI 和 PI)。
y
Dividend parameter
x
Divisor parameter
x
和 y
的反正切弧度值。
[#1] andyidol at gmail dot com [2010-08-10 08:18:10]
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;
}
?>
[#2] fred dot beck at rrd dot com [2009-01-11 12:39:01]
<?php
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).'°';
}
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);
?>
[#3] Monte Shaffer [2007-06-08 11:35:04]
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);
}
[#4] reubs at idsdatanet dot com [2003-05-23 13:01:59]
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. :)