gpt4 book ai didi

php - 给定 LatLong + Bearing 的直线交点

转载 作者:行者123 更新时间:2023-11-30 06:00:20 27 4
gpt4 key购买 nike

我正在尝试将一些 javascript 从 http://www.movable-type.co.uk/scripts/latlong.html 移植到 php .我让它运行时没有错误,但它给了我一个不同的值。我试着仔细检查这一切,最后又做了一遍,但结果相同。我不确定是不是我没有在某处错误地翻译某个函数还是什么。

JavaScript...

/**
* Returns the point of intersection of two paths defined by point and bearing
*
* see http://williams.best.vwh.net/avform.htm#Intersection
*
* @param {LatLon} p1: First point
* @param {Number} brng1: Initial bearing from first point
* @param {LatLon} p2: Second point
* @param {Number} brng2: Initial bearing from second point
* @returns {LatLon} Destination point (null if no unique intersection defined)
*/
LatLon.intersection = function(p1, brng1, p2, brng2) {
brng1 = typeof brng1 == 'number' ? brng1 : typeof brng1 == 'string' && trim(brng1)!='' ? +brng1 : NaN;
brng2 = typeof brng2 == 'number' ? brng2 : typeof brng2 == 'string' && trim(brng2)!='' ? +brng2 : NaN;
lat1 = p1._lat.toRad(), lon1 = p1._lon.toRad();
lat2 = p2._lat.toRad(), lon2 = p2._lon.toRad();
brng13 = brng1.toRad(), brng23 = brng2.toRad();
dLat = lat2-lat1, dLon = lon2-lon1;

dist12 = 2*Math.asin( Math.sqrt( Math.sin(dLat/2)*Math.sin(dLat/2) +
Math.cos(lat1)*Math.cos(lat2)*Math.sin(dLon/2)*Math.sin(dLon/2) ) );
if (dist12 == 0) return null;

// initial/final bearings between points
brngA = Math.acos( ( Math.sin(lat2) - Math.sin(lat1)*Math.cos(dist12) ) /
( Math.sin(dist12)*Math.cos(lat1) ) );
if (isNaN(brngA)) brngA = 0; // protect against rounding
brngB = Math.acos( ( Math.sin(lat1) - Math.sin(lat2)*Math.cos(dist12) ) /
( Math.sin(dist12)*Math.cos(lat2) ) );

if (Math.sin(lon2-lon1) > 0) {
brng12 = brngA;
brng21 = 2*Math.PI - brngB;
} else {
brng12 = 2*Math.PI - brngA;
brng21 = brngB;
}

alpha1 = (brng13 - brng12 + Math.PI) % (2*Math.PI) - Math.PI; // angle 2-1-3
alpha2 = (brng21 - brng23 + Math.PI) % (2*Math.PI) - Math.PI; // angle 1-2-3

if (Math.sin(alpha1)==0 && Math.sin(alpha2)==0) return null; // infinite intersections
if (Math.sin(alpha1)*Math.sin(alpha2) < 0) return null; // ambiguous intersection

//alpha1 = Math.abs(alpha1);
//alpha2 = Math.abs(alpha2);
// ... Ed Williams takes abs of alpha1/alpha2, but seems to break calculation?

alpha3 = Math.acos( -Math.cos(alpha1)*Math.cos(alpha2) +
Math.sin(alpha1)*Math.sin(alpha2)*Math.cos(dist12) );
dist13 = Math.atan2( Math.sin(dist12)*Math.sin(alpha1)*Math.sin(alpha2),
Math.cos(alpha2)+Math.cos(alpha1)*Math.cos(alpha3) )
lat3 = Math.asin( Math.sin(lat1)*Math.cos(dist13) +
Math.cos(lat1)*Math.sin(dist13)*Math.cos(brng13) );
dLon13 = Math.atan2( Math.sin(brng13)*Math.sin(dist13)*Math.cos(lat1),
Math.cos(dist13)-Math.sin(lat1)*Math.sin(lat3) );
lon3 = lon1+dLon13;
lon3 = (lon3+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180º

return new LatLon(lat3.toDeg(), lon3.toDeg());
}

还有我的 php...

<?php

function intersect($p1_lat, $p1_lon, $brng1, $p2_lat, $p2_lon, $brng2) {
$lat1 = deg2rad($p1_lat);
$lon1 = deg2rad($p1_lon);
$lat2 = deg2rad($p2_lat);
$lon2 = deg2rad($p2_lon);
$brng13 = deg2rad($brng1);
$brng23 = deg2rad($brng2);
$dLat = $lat2 - $lat1;
$dLon = $lon2 - $lon1;

$dist12 = 2 * asin(sqrt(sin($dLat / 2) * sin($dLat / 2) +
cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2)));
if ($dist12 == 0) {
return false;
}

// initial/final bearings between points
$brngA = acos(( sin($lat2) - sin($lat1) * cos($dist12) ) /
( sin($dist12) * cos($lat1) ));
if (is_nan($brngA)) {
$brngA = 0; // protect against rounding
}
$brngB = acos(( sin($lat1) - sin($lat2) * cos($dist12) ) /
( sin($dist12) * cos($lat2) ));

if (sin($lon2 - $lon1) > 0) {
$brng12 = $brngA;
$brng21 = 2 * pi() - $brngB;
} else {
$brng12 = 2 * pi() - $brngA;
$brng21 = $brngB;
}

$alpha1 = ($brng13 - $brng12 + pi()) % (2 * pi()) - pi(); // angle 2-1-3
$alpha2 = ($brng21 - $brng23 + pi()) % (2 * pi()) - pi(); // angle 1-2-3

if (sin($alpha1) == 0 && sin($alpha2) == 0) {
return false; // infinite intersections
}
if (sin($alpha1) * sin($alpha2) < 0) {
return false; // ambiguous intersection
}

$alpha3 = acos(-cos($alpha1) * cos($alpha2) +
sin($alpha1) * sin($alpha2) * cos($dist12));
$dist13 = atan2( sin($dist12)*sin($alpha1)*sin($alpha2),cos($alpha2)+cos($alpha1)*cos($alpha3) );
$lat3 = asin(sin($lat1) * cos($dist13) +
cos($lat1) * sin($dist13) * cos($brng13));
$dLon13 = atan2(sin($brng13) * sin($dist13) * cos($lat1), cos($dist13) - sin($lat1) * sin($lat3));
$lon3 = $lon1 + $dLon13;
$lon3 = ($lon3 + 3 * pi()) % (2 * pi()) - pi(); // normalise to -180..+180º

return array(rad2deg($lat3),rad2deg($lon3));
}

print_r (intersect(34.8403183513,-111.8159478164,148,34.8403254442,-111.8158955968,195));
?>

最佳答案

我试图从同一个库中移植另一个函数,但我遇到的问题是我需要使用 fmod() 而不是常规的 % 模数运算符。

关于php - 给定 LatLong + Bearing 的直线交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8977399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com