【问题标题】:Finding the closest point寻找最近的点
【发布时间】:2014-09-17 13:37:02
【问题描述】:

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

我没有在我命名为“访问”的数组中使用任何点。任何和所有的帮助表示赞赏!谢谢!

【问题讨论】:

    标签: java distance


    【解决方案1】:

    使用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

    【讨论】:

      【解决方案2】:

      你在这里拥有的是一个完美的封装候选者!我会首先考虑另一个对象来封装您一直提到的“点”概念:

      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。但如果没有,这应该这样做。然后您可以将这些点放入一个数据结构中(参见HashSet),如下所示:

      导入 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...");
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        相关资源
        最近更新 更多