如何找到最近的CGPoint并从NSArray创建一个集群?

时间:2017-02-18 05:49:27

标签: ios objective-c iphone markerclusterer cgpoint

我有一个场景,就像UIView中最近的CGPoint一样。所以我有一套CGPoint NSArray,我试图得到最接近的值并做集群,但我无法得到逻辑: //我的代码

  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));

    } 

上面的代码,获取重复点的循环时间也会覆盖同一个对象,所以如果有人知道这里的逻辑注释,..

1 个答案:

答案 0 :(得分: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];
            }
        }