gpt4 book ai didi

ios - 在 KD 树中搜索缓慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:02 25 4
gpt4 key购买 nike

我正在实现 KD 树以将 map 的点聚类成组。我一直在使用 Wikipedia's KD-tree article作为引用。搜索返回正确的最近邻点,但比我预期的要慢。这是我的代码:

- (FDRKDTree *)nnSearchForPoint:(id <MKAnnotation>)point best:(FDRKDTree *)best {
// consider the current node
distToPoint = [self distanceBetweenAnnotations:self.location and:point];
if (distToPoint < best.distToPoint) {
best = self;
}
// search near branch
int axis = depth % 2;
FDRKDTree *child = nil;
if (axis) {
if (point.coordinate.latitude > location.coordinate.latitude)
child = rightChild;
else
child = leftChild;
} else {
if (point.coordinate.longitude > location.coordinate.longitude)
child = rightChild;
else
child = leftChild;
}
if (child != nil)
best = [child nnSearchForPoint:point best:best];

child = nil;
//search the away branch - maybe
if (axis) {
if (fabs(point.coordinate.latitude - self.location.coordinate.latitude) <
best.distToPoint) {
if (point.coordinate.latitude > location.coordinate.latitude)
child = leftChild;
else
child = rightChild;
}
} else {
if (fabs(point.coordinate.longitude - self.location.coordinate.longitude) <
best.distToPoint) {
if (point.coordinate.longitude > location.coordinate.longitude)
child = leftChild;
else
child = rightChild;
}
}


if (child != nil) {
best = [child nnSearchForPoint:point best:best];
}

return best;
}

我的问题是如果我对“的解释是简单比较一下搜索点和当前节点的 split 坐标的差是否小于搜索点到当前最佳的距离(整体坐标) .”是正确的。我对此的解释是:

if (fabs(point.coordinate.latitude - self.location.coordinate.latitude) <
best.distToPoint)

if (fabs(point.coordinate.longitude - self.location.coordinate.longitude) <
best.distToPoint)

分别。也欢迎任何其他建议。

谢谢。

最佳答案

假设你的 distToPointsqrt((x1-x0)**2+(y1-y0)**2)。我在 Python 中实现了该算法,这可能有助于交叉检查您的版本并澄清一些维基百科文章要点: https://gist.github.com/863301

关于ios - 在 KD 树中搜索缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5134829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com