我終於解決了這個代碼:
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;
}
其中36.0是點和distanceFromView寬度:toView:是:
-(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));
}
希望幫助別人。