Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java X和Y坐标算法_Java_Algorithm - Fatal编程技术网

Java X和Y坐标算法

Java X和Y坐标算法,java,algorithm,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 有人知道,如何编写一个算法,为所有对象列表获取最近的

我有许多具有坐标的对象(如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

有人知道,如何编写一个算法,为所有对象列表获取最近的直线对象?

我假设坐标是大地坐标(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;
    }

你想做什么?我有公共汽车站的地图,用户给出他的坐标,算法给他一个最近的车站。所以计算从你的原点到所有点的距离,选择最近的一个。请不要问如何计算点之间的距离!!查哈弗森formula@FredK既然OP想找到最近的公共汽车站,我想我们可以假设距离足够短,不需要考虑地球的曲率。