2

我正在尝试使用Java中的Haversine公式计算大圆的公里距离,如下所示

/* Program to demonstrate the Floating-point numbers and the Math library.
 * The great-circle distance is the length of the shortest path between two points (x1,y1) and (x2,y2) on the surface of a sphere, where the path is constrained to be along the surface.*/
public class GreatCircle 
{
    public static void main(String[] args) 
    {
        double r = 6371.0; // Equatorial radius of the Earth
        double x1 = Math.toRadians(Double.parseDouble(args[0]));
        double y1 = Math.toRadians(Double.parseDouble(args[1]));
        double x2 = Math.toRadians(Double.parseDouble(args[2]));
        double y2 = Math.toRadians(Double.parseDouble(args[3]));

        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}

我正在运行 input java GreatCircle 60.0 15.0 120.0 105.0。预期的输出是4604.53989281927 kilometers,但我得到了13406.238676180266 kilometers。有人可以指出我哪里出错了吗?

4

3 回答 3

1

公式执行不正确。在进行以下更正后,它起作用了。在公式中,我们取整个表达式的反正弦。


        // Compute using Haversine formula
        double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));

        // Output the distance
        System.out.println(distance + " kilometers ");
    }
}
于 2021-12-24T09:57:04.670 回答
0

您忘记了计算Math.cos(x1) * Math.cos(x2),这就是您得到不同结果的原因。

// Compute using Haversine formula<br>
double distance = 2 * r * Math.asin(Math.sqrt((Math.pow(Math.sin((x2 - x1) / 2),2) + Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))));```

于 2021-12-27T13:53:05.003 回答
0
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2)
            + Math.cos(x2) * Math.cos(x1) * Math.pow(Math.sin((y2 - y1) / 2),2)));
于 2022-01-30T01:11:20.010 回答