mirror of https://github.com/grpc/grpc-java.git
routeguide: reimplement distance calculation
This commit is contained in:
parent
19a64cc336
commit
92f95de3cb
|
|
@ -268,25 +268,23 @@ public class RouteGuideServer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the distance between two points using the "haversine" formula.
|
* 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 start The starting point
|
||||||
* @param end The end point
|
* @param end The end point
|
||||||
* @return The distance between the points in meters
|
* @return The distance between the points in meters
|
||||||
*/
|
*/
|
||||||
private static int calcDistance(Point start, Point end) {
|
private static int calcDistance(Point start, Point end) {
|
||||||
double lat1 = RouteGuideUtil.getLatitude(start);
|
int r = 6371000; // earth radius in meters
|
||||||
double lat2 = RouteGuideUtil.getLatitude(end);
|
double lat1 = toRadians(RouteGuideUtil.getLatitude(start));
|
||||||
double lon1 = RouteGuideUtil.getLongitude(start);
|
double lat2 = toRadians(RouteGuideUtil.getLatitude(end));
|
||||||
double lon2 = RouteGuideUtil.getLongitude(end);
|
double lon1 = toRadians(RouteGuideUtil.getLongitude(start));
|
||||||
int r = 6371000; // meters
|
double lon2 = toRadians(RouteGuideUtil.getLongitude(end));
|
||||||
double phi1 = toRadians(lat1);
|
double deltaLat = lat2 - lat1;
|
||||||
double phi2 = toRadians(lat2);
|
double deltaLon = lon2 - lon1;
|
||||||
double deltaPhi = toRadians(lat2 - lat1);
|
|
||||||
double deltaLambda = toRadians(lon2 - lon1);
|
|
||||||
|
|
||||||
double a = sin(deltaPhi / 2) * sin(deltaPhi / 2)
|
double a = sin(deltaLat / 2) * sin(deltaLat / 2)
|
||||||
+ cos(phi1) * cos(phi2) * sin(deltaLambda / 2) * sin(deltaLambda / 2);
|
+ cos(lat1) * cos(lat2) * sin(deltaLon / 2) * sin(deltaLon / 2);
|
||||||
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
|
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
|
||||||
|
|
||||||
return (int) (r * c);
|
return (int) (r * c);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue