2017-02-18 5 views
-1

私は、UIViewで最も近いCGPointのクラスタを行うシナリオが1つあります。だから私は、私が最も近い値を取得しようとクラスタん、するCGPointにNSArrayの設定しているが、私はロジックを取得できませんでした: //私のコードの時間だけでなく上の上書き重複ポイントを取得するループコードの上NSArrayから最も近いCGPointを見つけてクラスタを作るには?

for (CGPoint firstObjOfCGPoint in cgPointGroupArray) {    

    for (CGPoint nextPoint in cgPointGroupArray) { 

    if (30>[self distanceBetween: firstObjOfCGPoint and:nextPoint]){     
       [shortestClusterArr addObject:nextPoint]; 
      } 
      else{ 
       [longestClusterArr addObject:nextPoint]; 
      } 
     } 
      if(shortestClusterArr.count>2){ 
      //clustered marker 
       [self addClusterMarker:shortestClusterArr]; 
      } 
      else{ 
       //dont cluster marker 
      } 
    } 
} 


    //find distance 

    - (float)distanceBetween:(CGPoint)p1 and:(CGPoint)p2 
    { 
     return hypotf((p1.x-p2.x), (p1.y-p2.y)); 

    } 

、同じオブジェクトなので、ここで誰かが論理的なコメントを知っていれば、..

答えて

0

オブジェクトを追加する前に、オブジェクトが存在するかどうかを確認する必要があります。このようなもの:

for (CGPoint nextPoint in cgPointGroupArray) { 
      if (30>[self distanceBetween:point and: nextPoint]) { 
       if (![shortestClusterArr containsObject:nextPoint]) 
        [shortestClusterArr addObject:nextPoint]; 
      } 
      else{ 
       if (![longestClusterArr containsObject:nextPoint]) 
        [longestClusterArr addObject:nextPoint]; 
      } 
     } 
関連する問題