gpt4 book ai didi

java - 计算两个任意形状之间的最小距离

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:01 25 4
gpt4 key购买 nike

我有两个任意形状。现在我想计算两个形状之间的最小距离。这里我附上图片

enter image description here

首先绘制部分完成。这种形状是弧和线的组合。现在,当我要计算这些形状之间的最小距离时,我遇到了问题。使用 GWT (java) html5 Canvas 绘制此形状。

为了计算两个形状之间的最小距离,我在 java 中使用了下面的代码,但我没有得到任何优化的方法来做到这一点 -

private double calculateMinimumDistance(Coordinate[] coordinates_1, Coordinate[] coordinates_2) { 
double minDistance = 100000;
double currentDistance = 0;

for(int i = 0; i < coordinates_1.length; ++i) {
for(int j = 0; j < coordinates_2.length; ++j) {
currentDistance = coordinates_1[i].distanceTo(coordinates_2[j]);
if(currentDistance < minDistance) {
minDistance = currentDistance;
}
}
}

return minDistance;
}

coordinates_1 包含 shape-1 的点集合。
coordinates_2 包含 shape-2 的点集合。

有没有优化的方法来计算两个形状之间的距离?这种形状可以是任何地方和任何类型的形状。

Instead of calculating the minimum distance between two set of point we can do it in optimized way by calculating distance between line to line or line to arc or arc to arc. In this way we can calculate the minimum distance in optimized way.

最佳答案

将这两种形状视为平面上两组不同的点。然后测量从第一组的每个点到第二组的每个点的距离。

为此使用嵌套的 for 循环并使用坐标几何的距离公式测量距离。

只存储最短的距离,如果你想要两点重合的距离。

关于java - 计算两个任意形状之间的最小距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646551/

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