gpt4 book ai didi

php - 在某个位置周围生成随机坐标

转载 作者:IT王子 更新时间:2023-10-28 23:58:18 26 4
gpt4 key购买 nike

我想要一个函数,它接受地理位置(纬度、经度)并在其周围生成随机坐标集,但也将这些参数作为计算的一部分:

  • 要生成的随机坐标数
  • 要生成的半径
  • 以米为单位的随机坐标之间的最小距离
  • 生成其周围位置的根坐标。

生成方式示例:

Example

实现这一目标的好方法是什么?

最佳答案

生成一个位置周围的随机坐标

function generateRandomPoint($centre, $radius) {
$radius_earth = 3959; //miles

//Pick random distance within $distance;
$distance = lcg_value()*$radius;

//Convert degrees to radians.
$centre_rads = array_map( 'deg2rad', $centre );

//First suppose our point is the north pole.
//Find a random point $distance miles away
$lat_rads = (pi()/2) - $distance/$radius_earth;
$lng_rads = lcg_value()*2*pi();


//($lat_rads,$lng_rads) is a point on the circle which is
//$distance miles from the north pole. Convert to Cartesian
$x1 = cos( $lat_rads ) * sin( $lng_rads );
$y1 = cos( $lat_rads ) * cos( $lng_rads );
$z1 = sin( $lat_rads );


//Rotate that sphere so that the north pole is now at $centre.

//Rotate in x axis by $rot = (pi()/2) - $centre_rads[0];
$rot = (pi()/2) - $centre_rads[0];
$x2 = $x1;
$y2 = $y1 * cos( $rot ) + $z1 * sin( $rot );
$z2 = -$y1 * sin( $rot ) + $z1 * cos( $rot );

//Rotate in z axis by $rot = $centre_rads[1]
$rot = $centre_rads[1];
$x3 = $x2 * cos( $rot ) + $y2 * sin( $rot );
$y3 = -$x2 * sin( $rot ) + $y2 * cos( $rot );
$z3 = $z2;


//Finally convert this point to polar co-ords
$lng_rads = atan2( $x3, $y3 );
$lat_rads = asin( $z3 );

return array_map( 'rad2deg', array( $lat_rads, $lng_rads ) );
}

用法

generateRandomPoint(array(3.1528, 101.7038), 4);

关于php - 在某个位置周围生成随机坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620550/

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