围绕位置生成随机坐标 [英] Generate random coordinates around a location

查看:833
本文介绍了围绕位置生成随机坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to have a function that accepts a geo location (Latitude, Longitude) and generates random sets of coordinates around it but also takes these parameters as a part of the calculation:

  • 要制作的随机坐标的数量
  • 要生成的半径
  • 随机坐标之间的最小距离(以米为单位)
  • 用于生成其周围位置的根坐标.

如何生成的示例:

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

What's a good approach to achieve this?

推荐答案

蛮力方法就足够了.

for each point to generate "n"
  find a random angle
  get the x and y from the angle * a random radius up to max radius
  for each point already generated "p"
     calculate the distance between "n" and "p"
  if "n" satisfies the min distance
     add new point "n"

在PHP中,生成新点很容易

In PHP, generating a new point is easy

$angle = deg2rad(mt_rand(0, 359));
$pointRadius = mt_rand(0, $radius);
$point = array(
   'x' => sin($angle) * $pointRadius,
   'y' => cos($angle) * $pointRadius
);

然后计算两点之间的距离

Then calculating the distance between two points

$distance = sqrt(pow($n['x'] - $p['x'], 2) + pow($n['y'] - $p['y'], 2));

** 编辑 **

为了弄清楚别人所说的话,并进行了进一步的研究(我不是数学家,但评论 did 让我感到疑惑),这里最简单的定义是高斯分布:

For the sake of clarifying what others have said, and after doing some further research (I'm not a mathematician, but the comments did make me wonder), here the most simple definition of a gaussian distribution :

如果尺寸为1维,则$ pointRadius = $ x * mt_rand(0, $ radius);会没事的,因为两者之间没有区别 当$ x具有高斯分布时,$ radius和$ x.

If you were in 1 dimension, then $pointRadius = $x * mt_rand(0, $radius); would be OK since there is no distinction between $radius and $x when $x has a gaussian distribution.

但是,如果坐标($ x,$ y,...)具有2个或更多维, 高斯分布,则半径$ radius不具有 高斯分布.

In 2 or more dimensions, however, if the coordinates ($x,$y,...) have gaussian distributions then the radius $radius does not have a gaussian distribution.

实际上$ radius ^ 2在2维中的分布[或k 维度]就是所谓的具有2 [或 k]自由度",条件是($ x,$ y,...)是独立的, 均值为零且方差相等.

In fact the distribution of $radius^2 in 2 dimensions [or k dimensions] is what is called the "chi-squared distribution with 2 [or k] degrees of freedom", provided the ($x,$y,...) are independent and have zero means and equal variances.

因此,要具有正态分布,必须将生成的半径的线更改为

Therefore, to have a normal distribution, you'd have to change the line of the generated radius to

$pointRadius = sqrt(mt_rand(0, $radius*$radius));

正如其他人所建议的.

这篇关于围绕位置生成随机坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆