-1

I have one scenario as do cluster for the nearest CGPoint in UIView. So i have set of CGPoint NSArray, I trying to get the nearest value and do cluster but i could not get the logic : // my code

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

    } 

Above code, looping time getting duplicate points as well override on the same object so if anyone knows the logic comment here,..

1 Answers1

0

You need to check if object already exist before adding them. Something like this:

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];
            }
        }
GeneCode
  • 7,545
  • 8
  • 50
  • 85