在彼此最近的三维空间中找到两个点

时间:2022-04-29 11:31:40

As the title suggests, I'm working on a homework assignment where we are limited to using multi-dimensional arrays in order to create a program that finds two points nearest to each other in a three dimensional space. So far my code looks like this (hybridized from examples in my textbook and my own code):

正如标题所示,我正在做一个家庭作业,我们只限于使用多维数组来创建一个程序,在三维空间中找到彼此最接近的两个点。到目前为止,我的代码看起来像这样(与我的教科书和我自己的代码中的示例混合):

package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Enter the number of points:");
    int numberOfPoints = input.nextInt();

    double[][]  points = new double[numberOfPoints][3];
    System.out.println("Enter " + numberOfPoints + " points:");
    for (int i = 0; i < points.length; i++) {
        points[i][0] = input.nextDouble();
        points[i][1] = input.nextDouble();
        points[i][2] = input.nextDouble();         
    }
    int p1 = 0, p2 = 1, p3 = 2;
    double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
            points[p2][0] , points[p2][1] , points[p2][2]);

    for (int i = 0; i < points.length; i++) {
        for (int j = i + 1; j < points.length; j++) {
            double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);

            if (shortestDistance > distance) {
                p1 = i;
                p2 = j;
                shortestDistance = distance;
            }
        }
    }
    System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] + 
        and (" + points[p2][0] + "," );
}
public static double distance(
        double x1, double y1, double z1, double x2, double y2, double z2) {
    return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));

  }
}

What I mostly need help with is figuring out just how to get these points compared. I don't think the way I tackled this problem was the best way to do it.

我最需要帮助的是弄清楚如何比较这些点。我不认为我解决这个问题的方法是最好的方法。

Thanks for the help guys. I'm running on 2 hours of sleep for 2 days now so please excuse any stupid questions or sloppy code.

谢谢你的帮助。我现在正在运行2小时的睡眠2天,所以请原谅任何愚蠢的问题或邋code的代码。

                                     ******

I think I've got it:

我想我已经得到了它:

package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Enter the number of points:");
    int numberOfPoints = input.nextInt();

    double[][]  points = new double[numberOfPoints][3];
    System.out.println("Enter " + numberOfPoints + " points:");
    for (int i = 0; i < points.length; i++) {
        points[i][0] = input.nextDouble();
        points[i][1] = input.nextDouble();
        points[i][2] = input.nextDouble();         
    }
    int p1 = 0, p2 = 1;
    double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
            points[p2][0] , points[p2][1] , points[p2][2]);

    for (int i = 0; i < points.length; i++) {
        for (int j = i + 1; j < points.length; j++) {
            double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);

            if (shortestDistance > distance) {
                p1 = i;
                p2 = j;
                shortestDistance = distance;
            }
        }
    }
    System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] + "," + points[p1][2] + 
            ") and (" + points[p2][0] + "," + points[p2][1] + "," + points[p2][2] + ")");
}
public static double distance(
        double x1, double y1, double z1, double x2, double y2, double z2) {
    return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));

}
}

Input is taken in, processed, and then outputs the two closest points. Just as a reference, when: (-1,0,3),(-1,-1,-1),(4,1,1),(2,0.5,9),(3.5,1.5,3),(-1.5,4,2),(5.5,4,-0.5) are inputted, the outcome seems to be (-1,0,3) and (4,1,1). Could someone confirm that for me.

输入被接收,处理,然后输出两个最接近的点。作为参考,当:( - 1,0,3),( - 1,-1,-1),(4,1,1),(2,0.5,9),(3.5,1.5,3)输入( - 1.5,4,2),(5.5,4,-0.5),结果似乎是(-1,0,3)和(4,1,1)。有人可以为我确认一下。

If this isn't the way to follow up on my own question, I apologize. First day on these slopes and I'm still learning the ropes.

如果这不是跟进我自己的问题的方法,我道歉。这些斜坡上的第一天,我还在学习绳索。

1 个解决方案

#1


2  

Use a class to represent your points. This way to you have a distanceTo method that calculates and returns distance. Also you can have a toString method that prints out the point for display to the user. Taking your code rearranging yields this class:

使用班级来表示您的积分。这种方式让你有一个distanceTo方法来计算和返回距离。您还可以使用toString方法打印出要显示给用户的点。重新排列代码会产生以下类:

public class ThreeDPoint {

    final double x;
    final double y;
    final double z;

    public ThreeDPoint(final double x, final double y, final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double distanceto(final ThreeDPoint other) {
        final double dx = other.x - x;
        final double dy = other.y - y;
        final double dz = other.z - z;

        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }

    @Override
    public String toString() {
        return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
    }
}

Now putting that together gives this, which is much more readable. I have removed the bit where you read points and used random numbers:

现在把它放在一起就可以了,这更具可读性。我删除了你读取点和使用随机数的位:

public static void main(String args[]) {
    final ThreeDPoint[] points = new ThreeDPoint[5];
    final Random random = new Random();
    for (int i = 0; i < points.length; ++i) {
        points[i] = new ThreeDPoint(random.nextInt(100), random.nextInt(100), random.nextInt(100));
    }
    //store min
    double min = Double.POSITIVE_INFINITY;
    int first = -1;
    int last = -1;
    for (int i = 0; i < points.length; ++i) {
        for (int j = i + 1; j < points.length; ++j) {
            final double d = points[i].distanceto(points[j]);
            if (d < min) {
                min = d;
                first = i;
                last = j;
            }
        }
    }
    System.out.println("The minimum distance is between point " + first + " and " + last + "(" + points[first] + " and " + points[last] + "). This distance is " + min + ".");
}

private static final class ThreeDPoint {

    final double x;
    final double y;
    final double z;

    public ThreeDPoint(final double x, final double y, final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double distanceto(final ThreeDPoint other) {
        final double dx = other.x - x;
        final double dy = other.y - y;
        final double dz = other.z - z;

        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }

    @Override
    public String toString() {
        return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
    }
}

#1


2  

Use a class to represent your points. This way to you have a distanceTo method that calculates and returns distance. Also you can have a toString method that prints out the point for display to the user. Taking your code rearranging yields this class:

使用班级来表示您的积分。这种方式让你有一个distanceTo方法来计算和返回距离。您还可以使用toString方法打印出要显示给用户的点。重新排列代码会产生以下类:

public class ThreeDPoint {

    final double x;
    final double y;
    final double z;

    public ThreeDPoint(final double x, final double y, final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double distanceto(final ThreeDPoint other) {
        final double dx = other.x - x;
        final double dy = other.y - y;
        final double dz = other.z - z;

        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }

    @Override
    public String toString() {
        return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
    }
}

Now putting that together gives this, which is much more readable. I have removed the bit where you read points and used random numbers:

现在把它放在一起就可以了,这更具可读性。我删除了你读取点和使用随机数的位:

public static void main(String args[]) {
    final ThreeDPoint[] points = new ThreeDPoint[5];
    final Random random = new Random();
    for (int i = 0; i < points.length; ++i) {
        points[i] = new ThreeDPoint(random.nextInt(100), random.nextInt(100), random.nextInt(100));
    }
    //store min
    double min = Double.POSITIVE_INFINITY;
    int first = -1;
    int last = -1;
    for (int i = 0; i < points.length; ++i) {
        for (int j = i + 1; j < points.length; ++j) {
            final double d = points[i].distanceto(points[j]);
            if (d < min) {
                min = d;
                first = i;
                last = j;
            }
        }
    }
    System.out.println("The minimum distance is between point " + first + " and " + last + "(" + points[first] + " and " + points[last] + "). This distance is " + min + ".");
}

private static final class ThreeDPoint {

    final double x;
    final double y;
    final double z;

    public ThreeDPoint(final double x, final double y, final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public double distanceto(final ThreeDPoint other) {
        final double dx = other.x - x;
        final double dy = other.y - y;
        final double dz = other.z - z;

        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }

    @Override
    public String toString() {
        return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
    }
}