这是我的Java实现,用于从QuadTree中获取最接近的实现。它处理了dlras2描述的问题:
我认为手术真的很有效。它基于到四边形的距离,以避免比当前最接近的四边形搜索。
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
public T getClosest(float x, float y) {
Closest closest = new Closest();
getClosest(x, y, closest);
return closest.item;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
protected void getClosest(float x, float y, Closest closestInfo) {
if (hasQuads) {
// we have no starting point yet
// so get one
if (closestInfo.item == null) {
// check all 4 cause there could be a empty one
for (int i = 0; i < 4; i++) {
quads[i].getClosest(x, y, closestInfo);
if (closestInfo.item != null) {
// now we have a starting point
getClosest(x, y, closestInfo);
return;
}
}
}
else {
// we have a item set as closest
// we should check if this quad is
// closer then the current closest distance
// let's start with the closest from index
int closestIndex = getIndex(x, y);
float d = quads[closestIndex].bounds.distToPointSQ(x, y);
if (d < closestInfo.dist) {
quads[closestIndex].getClosest(x, y, closestInfo);
}
// check the others
for (int i = 0; i < 4; i++) {
if (i == closestIndex) continue;
d = quads[i].bounds.distToPointSQ(x, y);
if (d < closestInfo.dist) {
quads[i].getClosest(x, y, closestInfo);
}
}
}
}
else {
for (int i = 0; i < items.size(); i++) {
T item = items.get(i);
float dist = distSQ(x, y, getXY.x(item), getXY.y(item));
if (dist < closestInfo.dist) {
closestInfo.dist = dist;
closestInfo.item = item;
closestInfo.tree = this;
}
}
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
class Closest {
QuadTree<T> tree;
T item;
float dist = Float.MAX_VALUE;
}