0

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

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)));

  }
}

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

谢谢你们的帮助。我现在连续 2 天睡 2 小时,所以请原谅任何愚蠢的问题或草率的代码。

                                     ******

我想我明白了:

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)));

}
}

输入被输入、处理,然后输出两个最近的点。仅作为参考,当:(-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)。有人可以为我确认一下。

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

4

1 回答 1

2

使用 aclass表示您的观点。这样你就有了一个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 + "}";
    }
}

现在把它放在一起给出了这个,它更具可读性。我已经删除了您读取点并使用随机数的位:

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 + "}";
    }
}
于 2013-09-19T11:04:15.420 回答