java - 寻找最近的点

标签 java distance

在我的程序中,我试图找到距起始位置 (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);
        }
    }
}

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

最佳答案

使用ArrayList存储点,

ArrayList<Double> x = new ArrayList<Double>();
ArrayList<Double> y = new ArrayList<Double>();

将点添加到数组列表,

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

从数组列表中选择点,

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

参见Class ArrayList

关于java - 寻找最近的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25881877/

相关文章:

java - 检测Spring框架中所有bean已经实例化

java - 如何使用 FFmpeg 限制阅读速度

opencv - 计算具有共享地平面的 2 个单应平面之间的距离

python - Python 中的欧几里得距离

distance - 短距离测量

ios - 通过 GPS 提高测量距离的准确性

java - Tomcat 上的证书配置

java - 如何从 HttpServlet 的 doPost() 方法返回一些数据?

java - EJB3 如何确保在删除 bean 之前发生事情

c# - 检测敌人是否面对玩家