在距给定位置一定距离范围内随机选择一个新位置

问题描述

我所拥有的:

  • 纬度/经度
  • 以千米为单位的最小距离,例如0.3
  • 以kms为单位的最大距离,例如1.5

我必须随机选择一个新位置,该位置至少应距给定位置0.3公里,最大距离1.5公里。即我必须选择的新位置可以在任意方向上距给定位置0.3公里至1.5公里之间。

注意:我必须用Elixir(编程)语言来实现,但是数学公式或伪代码都可以

解决方法

另一个answer帮助我在任意方向上均匀地随机生成一个位置。以下是步骤

  • dx = [0.3..0.7] x cos([0..2] x pi)
  • dy = [0.3..0.7] x sin([0..2] x pi)
  • this answer的一些帮助
  • r_earth = 6378

然后您就可以

new_latitude  = latitude  + (dy / r_earth) * (180 / pi);
new_longitude = longitude + (dx / r_earth) * (180 / pi) / cos(latitude * pi/180);

这是用Elixir语言实现的

def random_location_in_given_range(latitude,longitude,min_distance_in_km,max_distance_in_km) do
    random_distance =
    Enum.random(round(min_distance_in_km * 1000)..round(max_distance_in_km * 1000)) / 1000

    random_radian = Enum.random(0..200) / 100
    # dx = [0.3..0.7] x cos([0..2] x pi)
    dx = random_distance * :math.cos(random_radian * :math.pi())
    # dy = [0.3..0.7] x sin([0..2] x pi)
    dy = random_distance * :math.sin(random_radian * :math.pi())
    # radius of the earth in kilometer
    r_earth = 6378.137

    new_latitude = latitude + dy / r_earth * (180 / :math.pi())

    new_longitude =
    longitude + dx / r_earth * (180 / :math.pi()) / :math.cos(latitude * :math.pi() / 180)

    {new_latitude,new_longitude}
end

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...