简体   繁体   English

计算两点之间的距离 - Java

[英]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. 我知道lat long数据是十进制度数。 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 这是曼哈顿距离计算的变体,它不是真正的欧几里得斜边距离计算,而是直角三角形的两边的简单总和乘以某个乘数60.我通常认为它更简单地写为

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. 这基本上就是你的计算,除了你还要乘以一些比例因子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. 例如,我使用它一次非常粗糙(并且不正确但足够正确用于目的)计算像素颜色之间的差异,其中我必须对包含两个图像的像素重复进行实时计算,并且需要因为速度超过了对准确性的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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