From a26aecc03b5c110ce0912f3340933a4ed87d5ec0 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 22 Mar 2018 11:54:28 +0100 Subject: reimplement distance calculation in routeguide --- examples/ruby/route_guide/route_guide_server.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'examples/ruby') diff --git a/examples/ruby/route_guide/route_guide_server.rb b/examples/ruby/route_guide/route_guide_server.rb index 8ea07a21c5..5eb268b533 100755 --- a/examples/ruby/route_guide/route_guide_server.rb +++ b/examples/ruby/route_guide/route_guide_server.rb @@ -32,19 +32,18 @@ COORD_FACTOR = 1e7 RADIUS = 637_100 # Determines the distance between two points. +# The formula is based on http://mathforum.org/library/drmath/view/51879.html. def calculate_distance(point_a, point_b) to_radians = proc { |x| x * Math::PI / 180 } - lat_a = point_a.latitude / COORD_FACTOR - lat_b = point_b.latitude / COORD_FACTOR - long_a = point_a.longitude / COORD_FACTOR - long_b = point_b.longitude / COORD_FACTOR - φ1 = to_radians.call(lat_a) - φ2 = to_radians.call(lat_b) - Δφ = to_radians.call(lat_a - lat_b) - Δλ = to_radians.call(long_a - long_b) - a = Math.sin(Δφ / 2)**2 + - Math.cos(φ1) * Math.cos(φ2) + - Math.sin(Δλ / 2)**2 + lat_a = to_radians.call(point_a.latitude / COORD_FACTOR) + lat_b = to_radians.call(point_b.latitude / COORD_FACTOR) + lon_a = to_radians.call(point_a.longitude / COORD_FACTOR) + lon_b = to_radians.call(point_b.longitude / COORD_FACTOR) + delta_lat = lat_a - lat_b + delta_lon = lon_a - lon_b + a = Math.sin(delta_lat / 2)**2 + + Math.cos(lat_a) * Math.cos(lat_b) + + Math.sin(delta_lon / 2)**2 (2 * RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))).to_i end -- cgit v1.2.3