aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/ruby
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-22 11:54:28 +0100
committerGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-23 07:44:30 +0100
commita26aecc03b5c110ce0912f3340933a4ed87d5ec0 (patch)
treefd54656fc841298d4f8908972f8c4ce62396c30a /examples/ruby
parentb4a5727149201bac53a33c53c2cf93fed5414540 (diff)
reimplement distance calculation in routeguide
Diffstat (limited to 'examples/ruby')
-rwxr-xr-xexamples/ruby/route_guide/route_guide_server.rb21
1 files changed, 10 insertions, 11 deletions
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