首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找到最近的点

找到最近的点
EN

Stack Overflow用户
提问于 2014-09-17 03:37:59
回答 2查看 1.4K关注 0票数 0

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

代码语言:javascript
复制
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);
        }
    }
}

我没有在数组中使用任何点,我命名为“访问”。任何和所有的帮助都是感激的!谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-17 05:24:28

使用ArrayList存储点,

代码语言:javascript
复制
ArrayList<Double> x = new ArrayList<Double>();
ArrayList<Double> y = new ArrayList<Double>();

将点数加到列表中,

代码语言:javascript
复制
for( int i= 0; i< P; i++){ //Store the values from the text file
  x.add(StdIn.readDouble());
  y.add(StdIn.readDouble());
} 

从数组中选择点,

代码语言:javascript
复制
x.get(i); insted of x[i];
y.get(i); insted of y[i];

移除已经使用过的点,

代码语言:javascript
复制
x.remove(new Double(used_x_value));
y.remove(new Double(used_y_value));

请参阅类ArrayList

票数 1
EN

Stack Overflow用户

发布于 2014-09-17 04:24:05

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

代码语言:javascript
复制
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对。如果您这样做了,您可能需要重写哈希代码和相等。但如果不是这样的话,那就行了。然后,您可以将这些点放入数据结构(参见HashSet ),如下所示:

进口java.util.Set;进口java.util.HashSet;

代码语言:javascript
复制
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...");
        }

    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25881877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档