aboutsummaryrefslogtreecommitdiffhomepage
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
parentb4a5727149201bac53a33c53c2cf93fed5414540 (diff)
reimplement distance calculation in routeguide
-rw-r--r--examples/cpp/route_guide/route_guide_server.cc1
-rw-r--r--examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs21
-rw-r--r--examples/node/dynamic_codegen/route_guide/route_guide_server.js27
-rw-r--r--examples/node/static_codegen/route_guide/route_guide_server.js27
-rw-r--r--examples/python/route_guide/route_guide_server.py1
-rwxr-xr-xexamples/ruby/route_guide/route_guide_server.rb21
6 files changed, 45 insertions, 53 deletions
diff --git a/examples/cpp/route_guide/route_guide_server.cc b/examples/cpp/route_guide/route_guide_server.cc
index 24a4184a56..5867c16712 100644
--- a/examples/cpp/route_guide/route_guide_server.cc
+++ b/examples/cpp/route_guide/route_guide_server.cc
@@ -51,6 +51,7 @@ float ConvertToRadians(float num) {
return num * 3.1415926 /180;
}
+// The formula is based on http://mathforum.org/library/drmath/view/51879.html
float GetDistance(const Point& start, const Point& end) {
const float kCoordFactor = 10000000.0;
float lat_1 = start.latitude() / kCoordFactor;
diff --git a/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs b/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs
index 66c4a94573..f9af190888 100644
--- a/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs
+++ b/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs
@@ -52,26 +52,23 @@ namespace Routeguide
/// <summary>
/// Calculate the distance between two points using the "haversine" formula.
- /// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ /// The formula is based on http://mathforum.org/library/drmath/view/51879.html
/// </summary>
/// <param name="start">the starting point</param>
/// <param name="end">the end point</param>
/// <returns>the distance between the points in meters</returns>
public static double GetDistance(this Point start, Point end)
{
- double lat1 = start.GetLatitude();
- double lat2 = end.GetLatitude();
- double lon1 = start.GetLongitude();
- double lon2 = end.GetLongitude();
- int r = 6371000; // metres
- double phi1 = ToRadians(lat1);
- double phi2 = ToRadians(lat2);
- double deltaPhi = ToRadians(lat2 - lat1);
- double deltaLambda = ToRadians(lon2 - lon1);
+ int r = 6371000; // earth radius in metres
+ double lat1 = ToRadians(start.GetLatitude());
+ double lat2 = ToRadians(end.GetLatitude());
+ double lon1 = ToRadians(start.GetLongitude());
+ double lon2 = ToRadians(end.GetLongitude());
+ double deltalat = lat2 - lat1;
+ double deltalon = lon2 - lon1;
- double a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) + Math.Cos(phi1) * Math.Cos(phi2) * Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2);
+ double a = Math.Sin(deltalat / 2) * Math.Sin(deltalat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltalon / 2) * Math.Sin(deltalon / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
-
return r * c;
}
diff --git a/examples/node/dynamic_codegen/route_guide/route_guide_server.js b/examples/node/dynamic_codegen/route_guide/route_guide_server.js
index ab537ff401..f9028e860d 100644
--- a/examples/node/dynamic_codegen/route_guide/route_guide_server.js
+++ b/examples/node/dynamic_codegen/route_guide/route_guide_server.js
@@ -103,7 +103,7 @@ function listFeatures(call) {
/**
* Calculate the distance between two points using the "haversine" formula.
- * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ * The formula is based on http://mathforum.org/library/drmath/view/51879.html.
* @param start The starting point
* @param end The end point
* @return The distance between the points in meters
@@ -112,21 +112,18 @@ function getDistance(start, end) {
function toRadians(num) {
return num * Math.PI / 180;
}
- var lat1 = start.latitude / COORD_FACTOR;
- var lat2 = end.latitude / COORD_FACTOR;
- var lon1 = start.longitude / COORD_FACTOR;
- var lon2 = end.longitude / COORD_FACTOR;
- var R = 6371000; // metres
- var φ1 = toRadians(lat1);
- var φ2 = toRadians(lat2);
- var Δφ = toRadians(lat2-lat1);
- var Δλ = toRadians(lon2-lon1);
-
- var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
- Math.cos(φ1) * Math.cos(φ2) *
- Math.sin(Δλ/2) * Math.sin(Δλ/2);
+ var R = 6371000; // earth radius in metres
+ var lat1 = toRadians(start.latitude / COORD_FACTOR);
+ var lat2 = toRadians(end.latitude / COORD_FACTOR);
+ var lon1 = toRadians(start.longitude / COORD_FACTOR);
+ var lon2 = toRadians(end.longitude / COORD_FACTOR);
+
+ var deltalat = lat2-lat1;
+ var deltalon = lon2-lon1;
+ var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
+ Math.cos(lat1) * Math.cos(lat2) *
+ Math.sin(dlon/2) * Math.sin(dlon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
-
return R * c;
}
diff --git a/examples/node/static_codegen/route_guide/route_guide_server.js b/examples/node/static_codegen/route_guide/route_guide_server.js
index ef00bbbdfb..eecac62354 100644
--- a/examples/node/static_codegen/route_guide/route_guide_server.js
+++ b/examples/node/static_codegen/route_guide/route_guide_server.js
@@ -102,7 +102,7 @@ function listFeatures(call) {
/**
* Calculate the distance between two points using the "haversine" formula.
- * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ * The formula is based on http://mathforum.org/library/drmath/view/51879.html.
* @param start The starting point
* @param end The end point
* @return The distance between the points in meters
@@ -111,21 +111,18 @@ function getDistance(start, end) {
function toRadians(num) {
return num * Math.PI / 180;
}
- var lat1 = start.getLatitude() / COORD_FACTOR;
- var lat2 = end.getLatitude() / COORD_FACTOR;
- var lon1 = start.getLongitude() / COORD_FACTOR;
- var lon2 = end.getLongitude() / COORD_FACTOR;
- var R = 6371000; // metres
- var φ1 = toRadians(lat1);
- var φ2 = toRadians(lat2);
- var Δφ = toRadians(lat2-lat1);
- var Δλ = toRadians(lon2-lon1);
-
- var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
- Math.cos(φ1) * Math.cos(φ2) *
- Math.sin(Δλ/2) * Math.sin(Δλ/2);
+ var R = 6371000; // earth radius in metres
+ var lat1 = toRadians(start.getLatitude() / COORD_FACTOR);
+ var lat2 = toRadians(end.getLatitude() / COORD_FACTOR);
+ var lon1 = toRadians(start.getLongitude() / COORD_FACTOR);
+ var lon2 = toRadians(end.getLongitude() / COORD_FACTOR);
+
+ var deltalat = lat2-lat1;
+ var deltalon = lon2-lon1;
+ var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
+ Math.cos(lat1) * Math.cos(lat2) *
+ Math.sin(deltalon/2) * Math.sin(deltalon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
-
return R * c;
}
diff --git a/examples/python/route_guide/route_guide_server.py b/examples/python/route_guide/route_guide_server.py
index f10008fdec..1969fdd378 100644
--- a/examples/python/route_guide/route_guide_server.py
+++ b/examples/python/route_guide/route_guide_server.py
@@ -46,6 +46,7 @@ def get_distance(start, end):
delta_lat_rad = math.radians(lat_2 - lat_1)
delta_lon_rad = math.radians(lon_2 - lon_1)
+ # Formula is based on http://mathforum.org/library/drmath/view/51879.html
a = (pow(math.sin(delta_lat_rad / 2), 2) +
(math.cos(lat_rad_1) * math.cos(lat_rad_2) * pow(
math.sin(delta_lon_rad / 2), 2)))
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