如何解决在距给定位置一定距离范围内随机选择一个新位置
我所拥有的:
- 纬度/经度
- 以千米为单位的最小距离,例如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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。