我所拥有的:
我必须随机选择一个新位置,该位置至少应距给定位置0.3公里,最大距离1.5公里。即我必须选择的新位置可以在任意方向上距给定位置0.3公里至1.5公里之间。
注意:我必须用Elixir(编程)语言来实现,但是数学公式或伪代码都可以
答案 0 :(得分:0)
另一个answer帮助我在任意方向上均匀地随机生成一个位置。以下是步骤
dx = [0.3..0.7] x cos([0..2] x pi)
dy = [0.3..0.7] x sin([0..2] x pi)
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