Skip to content

Commit a26aecc

Browse files
committed
reimplement distance calculation in routeguide
1 parent b4a5727 commit a26aecc

File tree

6 files changed

+45
-53
lines changed

6 files changed

+45
-53
lines changed

examples/cpp/route_guide/route_guide_server.cc

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ float ConvertToRadians(float num) {
5151
return num * 3.1415926 /180;
5252
}
5353

54+
// The formula is based on http://mathforum.org/library/drmath/view/51879.html
5455
float GetDistance(const Point& start, const Point& end) {
5556
const float kCoordFactor = 10000000.0;
5657
float lat_1 = start.latitude() / kCoordFactor;

examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,23 @@ public static double GetLongitude(this Point point)
5252

5353
/// <summary>
5454
/// Calculate the distance between two points using the "haversine" formula.
55-
/// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
55+
/// The formula is based on http://mathforum.org/library/drmath/view/51879.html
5656
/// </summary>
5757
/// <param name="start">the starting point</param>
5858
/// <param name="end">the end point</param>
5959
/// <returns>the distance between the points in meters</returns>
6060
public static double GetDistance(this Point start, Point end)
6161
{
62-
double lat1 = start.GetLatitude();
63-
double lat2 = end.GetLatitude();
64-
double lon1 = start.GetLongitude();
65-
double lon2 = end.GetLongitude();
66-
int r = 6371000; // metres
67-
double phi1 = ToRadians(lat1);
68-
double phi2 = ToRadians(lat2);
69-
double deltaPhi = ToRadians(lat2 - lat1);
70-
double deltaLambda = ToRadians(lon2 - lon1);
62+
int r = 6371000; // earth radius in metres
63+
double lat1 = ToRadians(start.GetLatitude());
64+
double lat2 = ToRadians(end.GetLatitude());
65+
double lon1 = ToRadians(start.GetLongitude());
66+
double lon2 = ToRadians(end.GetLongitude());
67+
double deltalat = lat2 - lat1;
68+
double deltalon = lon2 - lon1;
7169

72-
double a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) + Math.Cos(phi1) * Math.Cos(phi2) * Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2);
70+
double a = Math.Sin(deltalat / 2) * Math.Sin(deltalat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltalon / 2) * Math.Sin(deltalon / 2);
7371
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
74-
7572
return r * c;
7673
}
7774

examples/node/dynamic_codegen/route_guide/route_guide_server.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function listFeatures(call) {
103103

104104
/**
105105
* Calculate the distance between two points using the "haversine" formula.
106-
* This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
106+
* The formula is based on http://mathforum.org/library/drmath/view/51879.html.
107107
* @param start The starting point
108108
* @param end The end point
109109
* @return The distance between the points in meters
@@ -112,21 +112,18 @@ function getDistance(start, end) {
112112
function toRadians(num) {
113113
return num * Math.PI / 180;
114114
}
115-
var lat1 = start.latitude / COORD_FACTOR;
116-
var lat2 = end.latitude / COORD_FACTOR;
117-
var lon1 = start.longitude / COORD_FACTOR;
118-
var lon2 = end.longitude / COORD_FACTOR;
119-
var R = 6371000; // metres
120-
var φ1 = toRadians(lat1);
121-
var φ2 = toRadians(lat2);
122-
var Δφ = toRadians(lat2-lat1);
123-
var Δλ = toRadians(lon2-lon1);
124-
125-
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
126-
Math.cos(φ1) * Math.cos(φ2) *
127-
Math.sin(Δλ/2) * Math.sin(Δλ/2);
115+
var R = 6371000; // earth radius in metres
116+
var lat1 = toRadians(start.latitude / COORD_FACTOR);
117+
var lat2 = toRadians(end.latitude / COORD_FACTOR);
118+
var lon1 = toRadians(start.longitude / COORD_FACTOR);
119+
var lon2 = toRadians(end.longitude / COORD_FACTOR);
120+
121+
var deltalat = lat2-lat1;
122+
var deltalon = lon2-lon1;
123+
var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
124+
Math.cos(lat1) * Math.cos(lat2) *
125+
Math.sin(dlon/2) * Math.sin(dlon/2);
128126
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
129-
130127
return R * c;
131128
}
132129

examples/node/static_codegen/route_guide/route_guide_server.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function listFeatures(call) {
102102

103103
/**
104104
* Calculate the distance between two points using the "haversine" formula.
105-
* This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
105+
* The formula is based on http://mathforum.org/library/drmath/view/51879.html.
106106
* @param start The starting point
107107
* @param end The end point
108108
* @return The distance between the points in meters
@@ -111,21 +111,18 @@ function getDistance(start, end) {
111111
function toRadians(num) {
112112
return num * Math.PI / 180;
113113
}
114-
var lat1 = start.getLatitude() / COORD_FACTOR;
115-
var lat2 = end.getLatitude() / COORD_FACTOR;
116-
var lon1 = start.getLongitude() / COORD_FACTOR;
117-
var lon2 = end.getLongitude() / COORD_FACTOR;
118-
var R = 6371000; // metres
119-
var φ1 = toRadians(lat1);
120-
var φ2 = toRadians(lat2);
121-
var Δφ = toRadians(lat2-lat1);
122-
var Δλ = toRadians(lon2-lon1);
123-
124-
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
125-
Math.cos(φ1) * Math.cos(φ2) *
126-
Math.sin(Δλ/2) * Math.sin(Δλ/2);
114+
var R = 6371000; // earth radius in metres
115+
var lat1 = toRadians(start.getLatitude() / COORD_FACTOR);
116+
var lat2 = toRadians(end.getLatitude() / COORD_FACTOR);
117+
var lon1 = toRadians(start.getLongitude() / COORD_FACTOR);
118+
var lon2 = toRadians(end.getLongitude() / COORD_FACTOR);
119+
120+
var deltalat = lat2-lat1;
121+
var deltalon = lon2-lon1;
122+
var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
123+
Math.cos(lat1) * Math.cos(lat2) *
124+
Math.sin(deltalon/2) * Math.sin(deltalon/2);
127125
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
128-
129126
return R * c;
130127
}
131128

examples/python/route_guide/route_guide_server.py

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def get_distance(start, end):
4646
delta_lat_rad = math.radians(lat_2 - lat_1)
4747
delta_lon_rad = math.radians(lon_2 - lon_1)
4848

49+
# Formula is based on http://mathforum.org/library/drmath/view/51879.html
4950
a = (pow(math.sin(delta_lat_rad / 2), 2) +
5051
(math.cos(lat_rad_1) * math.cos(lat_rad_2) * pow(
5152
math.sin(delta_lon_rad / 2), 2)))

examples/ruby/route_guide/route_guide_server.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,18 @@
3232
RADIUS = 637_100
3333

3434
# Determines the distance between two points.
35+
# The formula is based on http://mathforum.org/library/drmath/view/51879.html.
3536
def calculate_distance(point_a, point_b)
3637
to_radians = proc { |x| x * Math::PI / 180 }
37-
lat_a = point_a.latitude / COORD_FACTOR
38-
lat_b = point_b.latitude / COORD_FACTOR
39-
long_a = point_a.longitude / COORD_FACTOR
40-
long_b = point_b.longitude / COORD_FACTOR
41-
φ1 = to_radians.call(lat_a)
42-
φ2 = to_radians.call(lat_b)
43-
Δφ = to_radians.call(lat_a - lat_b)
44-
Δλ = to_radians.call(long_a - long_b)
45-
a = Math.sin(Δφ / 2)**2 +
46-
Math.cos(φ1) * Math.cos(φ2) +
47-
Math.sin(Δλ / 2)**2
38+
lat_a = to_radians.call(point_a.latitude / COORD_FACTOR)
39+
lat_b = to_radians.call(point_b.latitude / COORD_FACTOR)
40+
lon_a = to_radians.call(point_a.longitude / COORD_FACTOR)
41+
lon_b = to_radians.call(point_b.longitude / COORD_FACTOR)
42+
delta_lat = lat_a - lat_b
43+
delta_lon = lon_a - lon_b
44+
a = Math.sin(delta_lat / 2)**2 +
45+
Math.cos(lat_a) * Math.cos(lat_b) +
46+
Math.sin(delta_lon / 2)**2
4847
(2 * RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))).to_i
4948
end
5049

0 commit comments

Comments
 (0)