2016-05-23 15 views
0

Ich habe einen harten Algorithmus mit Blick auf ...kollidieren erweitert UIViews

Ich bin ein iOS-App zu entwickeln, wo ich einen Radius Karte habe, wo in der Mitte meines Profil-Bild gibt es und rund um das Profil-Bild gibt es andere Profile von anderen Benutzern Die Sache ist, dass einige dieser Profile sich überschneiden und ich möchte sie erweitern.

Mein Ziel ist es, das nächstgelegene Profil von mir zu bekommen und zu überprüfen, ob es ein anderes Profil gibt, das es überlappt. Wenn dies der Fall ist, nehmen Sie das Profil und verschieben Sie es vom anderen Profil weg.

The profiles overlaping

The profile moved from the nearest profile to the center

Kann mir jemand bitte helfen ...

Dank!

Antwort

0

ich endlich mit diesem Code gelöst:

int tries = 0; 
    while(true){ 
     BOOL f = NO; 
     for(Dot *dot1 in dots){ 

      for(Dot *dot2 in dots){ 

       if(dot1 == dot2) 
        continue; 

       CGFloat distance = fabs([self distanceFromView:dot1 toView:dot2]); 

       if(distance < 36.0){ 

        f = YES; 

        CGFloat bearing = atan2((dot2.suggestedCenter.y - dot1.suggestedCenter.y) , (dot2.suggestedCenter.x - dot1.suggestedCenter.x)); 

        CGPoint vector = CGPointMake(36.0 * cosf(bearing), 36.0 * sinf(bearing)); 

        CGPoint newPoint = CGPointMake(dot1.suggestedCenter.x + vector.x, dot1.suggestedCenter.y + vector.y); 
        dot2.suggestedCenter = newPoint; 

       } 

      } 

     } 
     if(!f) 
      break; 
     tries ++; 
     if(tries > 100) 
      break; 
    } 

Wo 36.0 die Breite des Punktes ist und distanceFromView: toview: ist:

-(double)distanceFromView:(Dot *)from toView:(Dot *)to{ 
     return sqrt(pow(to.suggestedCenter.x - from.suggestedCenter.x, 2) + pow(to.suggestedCenter.y - from.suggestedCenter.y, 2)); 
    } 

Hoffnung, dass jemand hilft.