我创建了3个数组,并且我用10个随机数(点)填充每个数组。现在我需要从每个阵列中取出点并计算3个点中2个点之间的最大距离。 I.E. x和z之间的距离大于x和y / z和y /等。我该怎么做这样的计算?
public static double max(double[] x, double[] y, double[] z) {
// Calculate the maximum distance
}
public static void main(String[] args) {
double xCoordArray = new double[10];
double yCoordArray = new double[10];
double zCoordArray = new double[10];
for (int i = 0; i < 10; i++) {
xCoordArray[i] = (int)(Math.random() * 10);
yCoordArray[i] = (int)(Math.random() * 10);
zCoordArray[i] = (int)(Math.random() * 10);
}
Point3D object0 = new Point32(xCoordArray[0], yCoordArray[0], zCoordArray[0]);
double maxDistance = max(xCoordArray, yCoordArray, zCoordArray);
System.out.println(object0);
System.out.println(maxDistance);
}
答案 0 :(得分:1)
编辑:顶部的新答案。老答案如下。
public class SimplepointsDistance {
public static void main(String[] args) {
double[] xCoordArray = new double[10];
double[] yCoordArray = new double[10];
double[] zCoordArray = new double[10];
for (int i = 0; i < 10; i++) {
xCoordArray[i] = (int)(Math.random() * 10);
yCoordArray[i] = (int)(Math.random() * 10);
zCoordArray[i] = (int)(Math.random() * 10);
}
int[] xyDistance = new int[10];
for(int i=0;i<10;i++){
xyDistance[i] = (int) Math.abs(xCoordArray[i] - yCoordArray[i]);
}
int[] xzDistance = new int[10];
for(int i=0;i<10;i++){
xzDistance[i] = (int) Math.abs(xCoordArray[i] - zCoordArray[i]);
}
int[] yzDistance = new int[10];
for(int i=0;i<10;i++){
yzDistance[i] = (int) Math.abs(yCoordArray[i] - zCoordArray[i]);
}
for (int i=0;i<10;i++){
int maxDistance = xyDistance[i];
int maxDistancePair = 1; //understand 1 means xy, 2 means xz, 3 means yz;
if (xzDistance[i] > maxDistance) {
maxDistance = xzDistance[i];
maxDistancePair = 2;
}
if (yzDistance[i] > maxDistance) {
maxDistance = yzDistance[i];
maxDistancePair = 3;
}
switch (maxDistancePair) {
case 1:
System.out.println("Greatest distance in set " + i + " is " + maxDistance + " between x and y");
break;
case 2:
System.out.println("Greatest distance in set " + i + " is " + maxDistance + " between x and z");
break;
case 3:
System.out.println("Greatest distance in set " + i + " is " + maxDistance + " between y and z");
break;
}
}
}
}
所以我们所做的就是记录对之间的距离(在我的类似xyDistance的数组中),然后遍历它们并从所有3对中挑出最大距离并显示它。
编辑:所以问题是在我处理答案的过程中编辑的,下面的代码确实不解决了问题,但我会保留它以防万一对其他任何人都派上用场了。我现在正在研究新答案。
你可以用毕达哥拉斯定理找到三维空间中两点之间的距离:
给定两个点A和B,其属性为x,y和z,它们之间的距离为:
squareroot( (A.x - B.x)^2 + (A.y - B.y)^2 + (A.z - B.z)^2 )
你的问题有点不清楚,你问了3分,但你似乎得到了10分。我假设你想要10点中任意2点之间的最大距离。
您需要循环浏览每组点并记录点和距离。
public class PointsDistance {
public static void main(String[] args) {
double[] xCoordArray = new double[10];
double[] yCoordArray = new double[10];
double[] zCoordArray = new double[10];
for (int i = 0; i < 10; i++) {
xCoordArray[i] = (int)(Math.random() * 10);
yCoordArray[i] = (int)(Math.random() * 10);
zCoordArray[i] = (int)(Math.random() * 10);
}
double distance[] = new double[45];
int setCounter = 0;
double maxDistance = 0;
int maxI = -1;
int maxJ = -1;
for (int i=0 ;i<9;i++){
for ( int j=i+1;j<10;j++){
distance[setCounter] = Math.sqrt ( Math.pow (xCoordArray[i] - xCoordArray[j], 2) + Math.pow (yCoordArray[i] - yCoordArray[j], 2) + Math.pow (zCoordArray[i] - zCoordArray[j], 2));
System.out.println("Testing " + i + " and " + j + " with distance " + distance[setCounter]);
if (distance[setCounter] > maxDistance) {
maxDistance = distance[setCounter ++];
maxI = i;
maxJ = j;
}
}
}
System.out.println("Maximum distance is " + maxDistance);
System.out.println("Between point " + maxI + ": " + xCoordArray[maxI] + ", " + yCoordArray[maxI] + ", " + zCoordArray[maxI]);
System.out.println("And " + maxJ + ": " + xCoordArray[maxJ] + ", " + yCoordArray[maxJ] + ", " + zCoordArray[maxJ]);
}
}
这里有一点解释:
double distance[] = new double[45];
45是9 + 8 + 7 ... + 1的总和,这是我需要匹配的对数:我不需要匹配任何一个点,所以第一点需要匹配以下9.第二点不需要针对第一点进行检查,因为已经完成了,所以我们根据以下内容进行检查8. for for循环中的条件(int i=0 ;i<9;i++
和{{1} })设置为最小化我们必须做的工作量。
之后,它只是记录最大的匹配并在最后显示它们。