X和Y带坐标 - 算法

时间:2016-08-11 17:20:28

标签: java algorithm

我有许多带坐标的对象(如600-1000),例如:

coordX - 16.88799654
coordY - 53.452535636

coordX - 16.78799652
coordY - 53.1436346423

coordX - 17.06546333
coordY - 52.96543332

.....

用户添加自己的坐标X和Y,例如:

userX = 12.4669945
userY = 52.234534536

有人有想法,如何编写算法,获取最近的直线对象列表中的所有对象?

1 个答案:

答案 0 :(得分:0)

我假设坐标是大地测量(lat / lon),因此简单的欧几里德距离不会是正确的。以下是C#,希望你能翻译成Java。如果需要,您还可以简化为单个功能。

    private const double EARTH_RADIUS_MI = 3959; //In statute miles

    public static float Radians(double degrees)
    {
        return (float)(degrees * (Math.PI / 180));
    }

    public static double CalculateDistance(Coordinates fromPoint, Coordinates toPoint)
    {
        double dLat = Radians(toPoint.Latitude - fromPoint.Latitude);
        double dLon = Radians(toPoint.Longitude - fromPoint.Longitude);

        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(Radians(fromPoint.Latitude)) * Math.Cos(Radians(toPoint.Latitude)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);

        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

        return EARTH_RADIUS_MI * c;
    }