Java 寻找最近的点

Java 寻找最近的点,java,distance,Java,Distance,在我的程序中,我试图从起始位置(0,0)找到最近的点,然后再次“移动”到下一个点。这些点通过文件读入。我试图移动到的下一个点是“最近的”点。我用毕达哥拉斯定理来求距离。但是,我可以做什么来“检查”我将要确定的点,如果我已经访问过它。例如,如果点为0,0,然后它变为1,1,如何检查以“告诉”程序0,0不再是选项 public class PointsNStuff { public static void main(String [] args) { final int P

在我的程序中,我试图从起始位置(0,0)找到最近的点,然后再次“移动”到下一个点。这些点通过文件读入。我试图移动到的下一个点是“最近的”点。我用毕达哥拉斯定理来求距离。但是,我可以做什么来“检查”我将要确定的点,如果我已经访问过它。例如,如果点为0,0,然后它变为1,1,如何检查以“告诉”程序0,0不再是选项

public class PointsNStuff {
    public static void main(String [] args) {

        final int P = StdIn.readInt();
        double [] x = new double[P];
        double [] y = new double[P];
        double [] visit= new double[P]; //Set an array that stores points that have been visited already
        double [] math= new double[P]; //Set an array that stores the distance to all the points


        for( int i= 0; i< P; i++){ //Store the values from the text file
            x[i] = StdIn.readDouble();
            y[i] = StdIn.readDouble();
        }

        double lowX = x[0];

        double lowY = y[0];

        double highX = x[0];

        double highY = y[0];

        //Find the lowest X and the lowest Y values:

        for (int i = 0; i < P; i++){
            if (lowX > x[i])
                lowX = x[i];
        }for (int i = 0; i < P; i++){
            if (lowY > y[i])
                lowY = y[i];
        }
        for (int i = 0; i < P; i++){
            if (highX < x[i])
                highX = x[i];
        }
        for (int i = 0; i < P; i++){
            if (highY < y[i])
                highY = y[i];
        }
        System.out.println(lowX + " " + lowY);
        System.out.println(highX + " " + highY);
        System.out.println("");
        System.out.println(P);

        //Determine the closest point
        double xCoord=0.0;
        double yCoord=0.0;
        double dist = -1.0;
        for (int i= 0; i < P; i ++){ //Repeat entire section for all P (number of points)
            for (int j = 0; j < P; j++){ //Find the distance between current point and all other points. Go through all points (do the math).
                xCoord = x[j]; // # x point
                yCoord = y[j]; // # y point
                double save= Math.sqrt( ( (xCoord+x[j]) * (xCoord+x[j]) ) + ( (yCoord + y[j]) * (yCoord + y[j]) ) ); //Pythagorean theorem
                save = math[j]; //store the distance in the array slot
            }
            for (int j = 0; j < P; j++){
                if (dist < math[j]){
                    dist = math[j];

                    //What boolean check can I put here to double check whether I have visited this point already?

                    xCoord = x[j]; // set the two points to what number they should be at.
                    yCoord = y[j];
                }
            }
            System.out.println(xCoord + " " + yCoord);
        }
    }
}
公共类pointsStuff{
公共静态void main(字符串[]args){
final int P=StdIn.readInt();
double[]x=新的double[P];
双精度[]y=新双精度[P];
double[]visit=new double[P];//设置存储已访问点的数组
double[]math=new double[P];//设置存储到所有点的距离的数组
对于(inti=0;ix[i])
lowX=x[i];
}对于(int i=0;iy[i])
lowY=y[i];
}
对于(int i=0;i

我没有在我命名为“访问”的数组中使用任何点。感谢您的任何帮助!谢谢

这里的内容非常适合封装!首先,我想用另一个对象来封装您一直提到的“点”概念:

class Point {
    private final double x;
    private final double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}
一个小警告:这假设输入文件中没有重复的x,y对。如果这样做,则可能需要重写hashcode和equals。但如果没有,这应该可以做到。然后可以将这些点放入如下数据结构(请参见):

导入java.util.Set; 导入java.util.HashSet

public class PointsNStuff {

    public static void main(String args[]) {

        Set<Point> pointsVisited = new HashSet<>();

        //when you visit a point, put it in the set like this
        //the numbers are just for example
        Point currentPoint = new Point(10.0, 12.0);
        pointsVisited.add(currentPoint);

        //now in the future you can check if you 'visited' this point
        if(!pointsVisited.contains(currentPoint)) {
            System.out.println("Haven't been to current point yet...");
        }

    }

}
公共类pointsStuff{
公共静态void main(字符串参数[]){
Set pointsVisited=new HashSet();
//当你访问一个点时,像这样把它放在集合中
//这些数字只是一个例子
点电流点=新点(10.0,12.0);
点visited.add(当前点);
//现在,将来您可以检查您是否“访问”了这一点
如果(!pointsVisited.contains(currentPoint)){
System.out.println(“尚未到达当前点…”);
}
}
}

使用ArrayList存储点

ArrayList<Double> x = new ArrayList<Double>();
ArrayList<Double> y = new ArrayList<Double>();
并移除已使用的点

x.remove(new Double(used_x_value));
y.remove(new Double(used_y_value));

x.get(i); insted of x[i];
y.get(i); insted of y[i];
x.remove(new Double(used_x_value));
y.remove(new Double(used_y_value));