rhumb行计算 - javascript到php

bri*_*ian 8 javascript php

此示例和java脚本代码来自链接文本
查看有关rhumb行的部分.


给定起始点和沿恒定方位θ的距离d,这将计算目标点.如果沿着垂直线保持恒定的方向,则会逐渐向一个极点旋转.

公式:

  ? = d/R (angular distance)   
  lat2 = lat1 + ?.cos(?)   
  ?? = ln(tan(lat2/2+?/4)/tan(lat1/2+?/4)) [= the ‘stretched’ latitude difference] 
if E:W line q = cos(lat1)    
otherwise q = ?lat/??   
  ?lon = ?.sin(?)/q   
  lon2 = (lon1+?lon+?) % 2.? ? ?   
  where ln is natural log and % is modulo, ?lon is taking shortest route (<180°), and R is the earth’s radius 
Run Code Online (Sandbox Code Playgroud)

JavaScript:

lat2 = lat1 + d*Math.cos(brng);
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);  // E-W line gives dPhi=0
var dLon = d*Math.sin(brng)/q;
// check for some daft bugger going past the pole, normalise latitude if so
if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -(Math.PI-lat2);
lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI; 
Run Code Online (Sandbox Code Playgroud)

我试图将其转换为PHP语法,但我没有得到所需的结果.我的纬度部分工作正常.我还包括了我的测试数据.


我的PHP代码

// test data
$R = 6371;
$tlatitude = 50.7;
$tlongitude = -105.214;
$theading = 124;
$d = 50;  

$projlat = $tlatitude +  rad2deg(($d/$R)*COS(deg2rad($theading)));

//?? = ln(tan(lat2/2+?/4)/tan(lat1/2+?/4))
$delta_phi = log(tan(deg2rad($projlat/2) + pi()/4)/(tan(deg2rad($tlatitude/2) + pi()/4)));

//q = ?lat/?? 
$delta_lat = deg2rad($projlat - $tlatitude);

$q = $delta_lat/$delta_phi;


//?lon = ?.sin(?)/q
$delta_long = rad2deg($d/$R*sin(deg2rad($theading))/$q);

$projlong = $tlongitude + $delta_long;
Run Code Online (Sandbox Code Playgroud)

我明白了 $projlong = -104.84

根据引用的页面,答案应该是-104.63.

现在我试图让这个工作无视东西方和极点的可能性.

Kei*_*eil 0

javascript 比 php 更精确看看这个笑话https://www.php.net/manual/en/function.doubleval.php

我曾经需要用 Luhn 算法检查一些 IBAN。我的 JavaScript 代码运行良好。但我的 php 失败了,所以经过一些研究,我发现了这个笑话,不得不根据字符串而不是数字重新编码基本操作(加、减、计算、除、模)。

也许您也应该重新编码,以获得预期的精度。我们不应该使用php进行高精度计算。