Java 计算三点之间的最大距离

Java 计算三点之间的最大距离,java,Java,我已经创建了3个数组,并用10个随机数(点)填充每个数组。现在我需要从每个数组中获取点,并计算3个点中2个点之间的最大距离。即,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) { doubl

我已经创建了3个数组,并用10个随机数(点)填充每个数组。现在我需要从每个数组中获取点,并计算3个点中2个点之间的最大距离。即,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);
}
公共静态双最大值(双x,双y,双z){
//计算最大距离
}
公共静态void main(字符串[]args){
double xCoordArray=新的双精度[10];
double yCoordArray=新的双精度[10];
double zCoordArray=新的双精度[10];
对于(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=新的Point32(xCoordArray[0],yCoordArray[0],zCoordArray[0]);
double maxDistance=max(xCoordArray、yCoordArray、zCoordArray);
System.out.println(object0);
System.out.println(最大距离);
}

编辑:新答案在顶部。旧答案如下

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;

        }



    }

}
你的问题有点不清楚,你问了大约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]);
    }
}

45是9+8+7的和…+1,这是我需要匹配的对数:我不需要将任何点与自身匹配,因此第一个点需要匹配以下9。第二点不需要对照第一点进行检查,因为已经完成了,所以我们对照下面的8点进行检查。我的for循环中的条件(
int i=0;i你知道如何计算点之间的距离吗?你知道如何排序吗?三个点
(x,y,z)
。不是一个(3D)点?你想计算点之间的距离还是轴值?请将你的问题表达得更清楚。@Sotirios,不,我不知道如何计算点之间的距离,是的,我知道如何排序。@Yohanes,我将重新表述我的问题。然后我建议你研究如何计算点之间的距离。谢谢你,Dev我知道我的问题不清楚,我在解释自己时遇到了困难。我很感谢你的解释,现在我有一些研究要做。
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];