简体   繁体   中英

Calculate Distance between two points- Java

I have inherited a code and it has this small function written to calculate distance between two points.I'm wondering , what it does. I know the lat long data is in decimal degrees. Could anyone please throw some insights, if this calculation is right?

     private double calculateDistance(QuoteItem quoteItem, RouteInfo routeInfo) {
        final double distance =
           ((Math.max(routeInfo.getLatitude(), quoteItem.getLatitude()) - Math.min    (routeInfo.getLatitude(), quoteItem.getLatitude())) +
           (Math.max(routeInfo.getLongitude(), quoteItem.getLongitude()) - Math.min(routeInfo.getLongitude(), quoteItem.getLongitude()))) * 60.0;
return distance;
     }

This is a variant of the Manhattan distance calculation where it's not a true Euclidean hypotenuse distance calculation, but rather a simple sum of the two sides of the right triangle multiplied by some multiplier, 60. I usually see it written more simply as

Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y)

在此输入图像描述

Which is essentially what your calculation is, except you're also multiplying it by some scaling factor, 60.0.

I've used this in programs where I want to get a quick and dirty estimate of distance with an emphasis on quick since it involves no square roots. For instance, I used it once for very rough (and incorrect but correct enough for the purposes) calculation of the differences between pixel colors, where I had to make this calculation repeatedly for the pixels contained two images, in real time, and where need for speed trumped the need for accuracy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM