Nil*_*tel 3 php math linear-algebra
我想找到矢量之间的角度
v1 = [-1,-2]
和
v2 = [90,-5]
在PHP代码中需要计算两个向量之间的角度 [-1,-2] and [90,-5].需要PHP代码.
谢谢
function norm($vec)
{
$norm = 0;
$components = count($vec);
for ($i = 0; $i < $components; $i++)
$norm += $vec[$i] * $vec[$i];
return sqrt($norm);
}
function dot($vec1, $vec2)
{
$prod = 0;
$components = count($vec1);
for ($i = 0; $i < $components; $i++)
$prod += ($vec1[$i] * $vec2[$i]);
return $prod;
}
Run Code Online (Sandbox Code Playgroud)
并计算实际角度:
$v1 = array(-1, -2);
$v2 = array(90, -5);
$ang = acos(dot($v1, $v2) / (norm($v1) * norm($v2)));
echo $ang; // angle in radians
> 1.97894543055
Run Code Online (Sandbox Code Playgroud)
你可以使用atan2($y,$x)php中的函数来完成它.找到弧度的角度.
<?php
$angle = rad2deg(atan2($y2-$y1,$x2-$x1));
//$angle is in degrees
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5237 次 |
| 最近记录: |