Ruby 代码片断-计算两点之间的距离
2006年9月15日星期五
根据两点的经纬度计算两点之间的距离:
# converts degrees to radians
# params: degree to convert
# returns: answer in radians
def self.convert_to_radians(degree_num)
radians = degree_num.to_f * Math::PI/180
end# calculate the distance between two zips using the great circle ormula
# params: orig latitude, orig longitude, to latitude, to longitude
# returns: distance in miles
def self.calc_distance_between(orig_lat, orig_long, to_lat, to_long)
# find the delta’s of latitude and longitude
delta_lat = to_lat - orig_lat
delta_long = to_long - orig_long# compute the sin^2(delta_*/2)
computed_lat = Math.sin(delta_lat/2)
computed_lat = computed_lat * computed_lat
computed_long = Math.sin(delta_long/2)
computed_long = computed_long * computed_long# find the inner part of the great circle
inner_equation = computed_lat + Math.cos(orig_lat) * Math.cos(to_lat) * computed_long
# find the outer part of the great circle
outer_equation = 2 * Math.atan2(Math.sqrt(inner_equation),Math.sqrt(1-inner_equation))
# find the distance in miles
distance = 3956 * outer_equation
end
标签: Technologies
发表评论
订阅 博文评论 [Atom]
<< 主页