Calculate the Distance Between Two Points in Java – 在Java中计算两点之间的距离

最后修改: 2018年 9月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll show how to calculate the distance between two points in Java.

在这个快速教程中,我们将展示如何用Java计算两点之间的距离。

2. The Math Formula of the Distance

2.距离的数学公式

Let’s say we have two points on a plane: the first point A has the coordinates (x1, y1), and the second point B has the coordinates (x2, y2). We want to calculate AB, the distance between the points.

假设我们在一个平面上有两个点:第一个点A的坐标是(x1,y1),第二个点B的坐标是(x2,y2)。我们想计算AB,即两点之间的距离。

Firstly, let’s build a right triangle with the hypotenuse AB:

首先,让我们建立一个斜边为AB的直角三角形。

triangle

According to the Pythagorean theorem, the sum of the squares of the lengths of the triangle’s legs is the same as the square of the length of the triangle’s hypotenuse: AB2 = AC2 + CB2.

根据毕达哥拉斯定理,三角形的腿长的平方之和与三角形斜边长度的平方相同。AB2 = AC2 + CB2

Secondly, let’s calculate AC and CB.

其次,让我们计算一下AC和CB。

Obviously:

很明显。

AC = y2 - y1

Similarly:

同样地。

BC = x2 - x1

Let’s substitute the parts of the equation:

让我们把方程的各部分代入。

distance * distance = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)

Finally, from the above equation we can calculate the distance between the points:

最后,从上述公式中我们可以计算出两点之间的距离。

distance = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1))

Now let’s move on to the implementation part.

现在让我们进入实施部分。

3. Java Implementation

3.Java实现

3.1. Using Plain Formula

3.1.使用普通公式

Although java.lang.Math and java.awt.geom.Point2D packages provide ready solutions, let’s firstly implement the above formula as is:

虽然java.lang.Mathjava.awt.geom.Point2D包提供了现成的解决方案,但我们首先要按原样实现上述公式。

public double calculateDistanceBetweenPoints(
  double x1, 
  double y1, 
  double x2, 
  double y2) {       
    return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}

To test the solution, let’s take the triangle with legs 3 and 4 (as shown in the picture above).  It’s clear that the number 5 is suitable as the value of the hypotenuse:

为了检验解决方案,让我们以腿34的三角形为例(如上图所示)。 很明显,数字5适合作为斜边的值。

3 * 3 + 4 * 4 = 5 * 5

Let’s check the solution:

让我们检查一下解决方案。

@Test
public void givenTwoPoints_whenCalculateDistanceByFormula_thenCorrect() {
    double x1 = 3;
    double y1 = 4;
    double x2 = 7;
    double y2 = 1;

    double distance = service.calculateDistanceBetweenPoints(x1, y1, x2, y2);

    assertEquals(distance, 5, 0.001);
}

3.2. Using java.lang.Math Package

3.2.使用java.lang.Math软件包

If the result of multiplication in the calculateDistanceBetweenPoints() method is too big, overflow can occur. Unlike that, Math.hypot() method prevents intermediate overflow or underflow:

如果calculateDistanceBetweenPoints()方法中的乘法结果太大,就会发生溢出。与此不同的是,Math.hypot()方法可以防止中间的溢出或下溢。

public double calculateDistanceBetweenPointsWithHypot(
    double x1, 
    double y1, 
    double x2, 
    double y2) {
        
    double ac = Math.abs(y2 - y1);
    double cb = Math.abs(x2 - x1);
        
    return Math.hypot(ac, cb);
}

Let’s take the same points as before and check that the distance is the same:

让我们采取与之前相同的点,检查距离是否相同。

@Test
public void givenTwoPoints_whenCalculateDistanceWithHypot_thenCorrect() {
    double x1 = 3;
    double y1 = 4;
    double x2 = 7;
    double y2 = 1;

    double distance = service.calculateDistanceBetweenPointsWithHypot(x1, y1, x2, y2);

    assertEquals(distance, 5, 0.001);
}

3.3. Using java.awt.geom.Point2D Package

3.3.使用java.awt.geom.Point2D软件包

Finally, let’s calculate the distance with the Point2D.distance() method:

最后,让我们用Point2D.distance()方法来计算距离。

public double calculateDistanceBetweenPointsWithPoint2D( 
    double x1, 
    double y1, 
    double x2, 
    double y2) {
        
    return Point2D.distance(x1, y1, x2, y2);
}

Now let’s test the method in the same way:

现在让我们以同样的方式测试一下这个方法。

@Test
public void givenTwoPoints_whenCalculateDistanceWithPoint2D_thenCorrect() {

    double x1 = 3;
    double y1 = 4;
    double x2 = 7;
    double y2 = 1;

    double distance = service.calculateDistanceBetweenPointsWithPoint2D(x1, y1, x2, y2);

    assertEquals(distance, 5, 0.001);
}

4. Conclusion

4.结论

In this tutorial, we’ve shown a few ways to calculate the distance between two points in Java.

在本教程中,我们展示了几种在Java中计算两点之间距离的方法。

As always, the code used in the examples is available over on GitHub.

一如既往,示例中使用的代码可在GitHub上获得over