diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2018-03-22 12:43:05 +0100 |
---|---|---|
committer | Eric Anderson <ejona@google.com> | 2018-03-23 08:25:26 -0700 |
commit | 92f95de3cb29223a3ddf18f2d2eb034b7fd219d6 (patch) | |
tree | f3168166bb055922543bb3f28c18308a1df04373 /examples | |
parent | 19a64cc336146996609dff266d15381d8fbe2fea (diff) | |
download | grpc-grpc-java-92f95de3cb29223a3ddf18f2d2eb034b7fd219d6.tar.gz |
routeguide: reimplement distance calculation
Diffstat (limited to 'examples')
-rw-r--r-- | examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java b/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java index cf7ebe15e..4d9cd201e 100644 --- a/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java +++ b/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java @@ -268,25 +268,23 @@ public class RouteGuideServer { /** * 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 */ private static int calcDistance(Point start, Point end) { - double lat1 = RouteGuideUtil.getLatitude(start); - double lat2 = RouteGuideUtil.getLatitude(end); - double lon1 = RouteGuideUtil.getLongitude(start); - double lon2 = RouteGuideUtil.getLongitude(end); - int r = 6371000; // meters - double phi1 = toRadians(lat1); - double phi2 = toRadians(lat2); - double deltaPhi = toRadians(lat2 - lat1); - double deltaLambda = toRadians(lon2 - lon1); - - double a = sin(deltaPhi / 2) * sin(deltaPhi / 2) - + cos(phi1) * cos(phi2) * sin(deltaLambda / 2) * sin(deltaLambda / 2); + int r = 6371000; // earth radius in meters + double lat1 = toRadians(RouteGuideUtil.getLatitude(start)); + double lat2 = toRadians(RouteGuideUtil.getLatitude(end)); + double lon1 = toRadians(RouteGuideUtil.getLongitude(start)); + double lon2 = toRadians(RouteGuideUtil.getLongitude(end)); + double deltaLat = lat2 - lat1; + double deltaLon = lon2 - lon1; + + double a = sin(deltaLat / 2) * sin(deltaLat / 2) + + cos(lat1) * cos(lat2) * sin(deltaLon / 2) * sin(deltaLon / 2); double c = 2 * atan2(sqrt(a), sqrt(1 - a)); return (int) (r * c); |