Doestovsky
Doestovsky

Reputation: 65

Find Two Points In A Three-Dimensional Space Nearest To Each Other

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.

                                     ******

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.

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.

Upvotes: 0

Views: 2798

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61148

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:

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 + "}";
    }
}

Upvotes: 2

Related Questions