2011-02-27 5 views
2

私はクラスターにマップをグループにグループ化するKDツリーを実装しています。私はWikipedia's KD-tree articleを参考にしています。検索は正しい最近隣の点を返しますが、予想よりも遅いです。ここに私のコードは次のとおりです。KD-treeでの検索遅い

- (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; 
} 

"の私の解釈分裂との差が、探索点の座標と現在のノードかどうかを確認するための単純な比較は距離(全体的な座標)未満であれば私の質問があるから検索ポイントが現在のベスト。 "が正しいです。私はこのように解釈する:それぞれ

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

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

。他のアドバイスも歓迎します。

ありがとうございました。

答えて

0

distToPointsqrt((x1-x0)**2+(y1-y0)**2)であると仮定すると、私にはかなりうまく見えます。私はPythonでアルゴリズムを実装していますので、あなたのバージョンをクロスチェックし、Wikipediaの記事のポイントを明確にすることができます: https://gist.github.com/863301

関連する問題