gpt4 book ai didi

java - 3 维房间中的欧几里得距离 - 基于 Java 中的 2d 数组

转载 作者:行者123 更新时间:2023-12-02 09:21:13 24 4
gpt4 key购买 nike

我想计算存储在二维数组中的三维房间中的点之间的距离。这些点是随机生成的,我们假设所有点都可能位于 3d 空间中。这些点将具有三个参数,如下所示:[[3.0, 5.0, 8.0], [6.0, 5.0, 4.0], [3.0, 3.0, 9.0], [8.0, 0.0, 3.0]]

我想我已经走了这么远,但我似乎错误地进入了数组。您有什么建议吗?我的索引超出了范围。

公开课积分{

public static class points {

static String distanceBetweenPoints(int a) {
// int a = number of Points

int columns = 3;
// every point has three coordinates x,y,z

double[][] points = new double[a][columns];

// generating the points with random method:

for (int i = 0; i < a; i++) {
for (int j = 0; j < columns; j++) {
points[i][j] = (int) (Math.random() * 10);
}
}

double[][] distances = new double[points.length][points.length];

for (int i = 0; i < distances.length; i++) {
for (int j = 0, k = 0; j < distances.length; j++ )
for (int l = 0, m = 0; j < points[0].length; l++, m++) {
distances[i][j] = Math.sqrt((Math.pow((points[i][k] - points[l][k]),2)
+ Math.pow((points[j][1] - points[l][1]), 2)
+ Math.pow((points[j][2] - points[l][2]), 2)));

}
}

return Arrays.deepToString(distances);
}

public static void main(String[] args) {

System.out.println(distanceBetweenPoints(4));
}
}

}

最佳答案

放一个简单的

System.out.println( String.format( "i: %d, j: %d, k: %d, l: %d, m: %d", i, j, k, l, m) ); 

进入你的代码会立即显示你的错误。在内循环的第四次迭代中,l 变为 4,大于点数组的大小。

在哪个循环中声明、检查和递增索引的情况一团糟。

关于java - 3 维房间中的欧几里得距离 - 基于 Java 中的 2d 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680782/

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