gpt4 book ai didi

java - 如何知道两个坐标之间的距离?

转载 作者:行者123 更新时间:2023-12-01 18:00:31 25 4
gpt4 key购买 nike

也许这不是我要寻找答案的网站,但这是我的问题。

我正在开发一个 Java 程序,我需要知道两个位置之间的距离(以米为单位),因为它的坐标格式为 EPSG:4326。

例如,

坐标1:

42.34839, 2.484839

坐标2:

42.27345, 2.23453

从数学上来说,知道两个坐标之间的距离差的系统是什么?

最佳答案

有大量不同的算法用于确定两点之间的距离。例如,曼哈顿距离非常简单 IIRC,它只是 abs(x1 - x2) + abs(y1 - y2)。

class Coordinate {

private final float x;

private final float y;

Coordinate(float x, float y) {
this.x = x;
this.y = y;
}

public float manhattanDistance(Coordinate other) {
return Math.abs(x - other.x) + Math.abs(y - other.y);
}

public float getX() {
return x;
}

public float getY() {
return y;
}
}
Coordinate first = new Coordinate(42.34839f, 2.484839f);

Coordinate second = new Coordinate(42.27345f, 2.23453f);

float manhattanDistance = first.manhattanDistance(second);

System.out.println("distance: " + manhattanDistance);

输出

distance: 0.32524872

关于java - 如何知道两个坐标之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60642060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com