如何从列表中获取最近的Vector到给定目标

时间:2016-11-25 22:55:08

标签: java opengl

想象一下,我在Java中创建了一个Vector类,其中包含两个变量xy

public class Vector {
    private int x;
    private int y;

    public Vector(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;  
    }

    public int getY(){
        return this.y;
    }
}

然后我发起了ArrayList个向量:

private List<Vector> vecs = new ArrayList<Vector>();

我在该列表中创建了:

8,9
10,5
83473834,938849584985
etc ...

现在我想获得最接近另一个向量的向量。 例如:

private List<Vector> vecs = new ArrayList<Vector>();
private Vector vec = new Vector(1,1);

for(Vector vector:vecs) {
    //What do i put here??
}

那么我在for循环中放入什么来使它从矢量列表中选择最近的矢量?

2 个答案:

答案 0 :(得分:2)

我首先向Vector类添加一个方法,distanceTo,计算从这个向量到另一个向量的距离:

public double distanceTo(Vector vec) {
    double dx = x - vec.x;               //calculate the diffrence in x-coordinate
    double dy = y - vec.y;               //calculate the diffrence in y-coordinate
    return Math.sqrt(dx*dx + dy*dy);     //use the distance formula to find the difference
}

然后您可以编写以下方法,将列表中最接近的向量返回给定向量:

public static Vector closest(Vector target, List<Vector> list) {
    Vector closest = list.get(0);                                 //this variable will kep track of the closest vector we have found yet. We simply start with the first one

    for(int i = 1; i < list.size(); i++) {                        //loop over the list, skipping the first entry
        Vector curr = list.get(i);                                //get the current vector from the list
        if (target.distanceTo(curr) < target.distanceTo(closest))    //if the current vector is closer to target than the closest one yet
            closest = curr;                                       //keep the current vector as the new closest one
    }

    return closest;                                               //return the resulting vector
}

然后可以像这样使用此方法:

Vector target = new Vector(1, 2);

List<Vector> vecs = new ArrayList<Vector>();
vecs.add(new Vector(-2, 6));
vecs.add(new Vector(1, 3));
vecs.add(new Vector(4, 0));
vecs.add(new Vector(8, -1));

Vector closest = findClosest(target, vecs);

正如您所见,我尽可能地尽力解释代码,但随时可以提出更多问题!

编辑另一种方法是:

 public double distanceTo(Vector vec1,Vector vec2) {
        double dx = vec2.x - vec1.x;               //calculate the diffrence in x-coordinate
        double dy = vec.y - vec1.y;               //calculate the diffrence in y-coordinate
        return Math.sqrt(dx*dx + dy*dy);     //use the distance formula to find the difference
    }

如果你不能将它放入矢量类

答案 1 :(得分:2)

这是一个基本的编程问题。它与OpenGL无关。简单的线性搜索可能如下所示:

belongTo

之后,您将在private List<Vector> vecs = new ArrayList<Vector>(); private Vector vec = new Vector(1,1); Vector minDistanceVector = null; int minDistanceSquared = Integer.MAX_VALUE; for(Vector vector : vecs) { //Calculate the distance //This could be a member function of Vector int dx = vector.getX() - vec.getX(); int dy = vector.getY() - vec.getY(); int squaredDistance = dx * dx + dy * dy; if(squaredDistance < minDistanceSquared) { minDistanceSquared = squaredDistance; minDistanceVector = vector; } } 中拥有最接近的向量。我选择欧几里德距离,因为这可能是你想要的。但是你当然可以使用任何其他距离。

如果你想要更高效的东西,你可能想要在点上构建一些加速度数据结构并查询那个(例如网格,kd树,四叉树......)。