2012-04-08 3 views
1

임의의 위치에있는 원 수가 많습니다. 내가 막기를 원하는 것은 서클들이 서로 중첩되지 않도록하는 것입니다. 그래서 새로 생성 된 CGPoint 값을 배열에있는 값과 비교하고 값이 CGPoint 중 하나의 반경 영역에서 발견되면 새로운 값을 생성하려고합니다.새 CGPoint 값을 배열의 CGPoint 값과 비교합니다.

나는 겹쳐진 것을 제외하고는 대부분 가지고있다. 나는 내가 해보았던 부분에 대해 논평했지만 실제로 작동하지는 않습니다 (결함있는 논리). CGPoint의 개별 값을 반복하고 비교하는 방법을 모르겠습니다.

업데이트 : 코드를 조금 더 변경하고 CGPoint가 중첩되는지 여부를 확인했습니다. 내가

while (!xPointOk || !yPointOk) { 

// generate new values 

} 

내가 무한 루프에 빠지는 계속 뭔가를 시도 할 때마다 문제입니다. 당신이 그것을 해결하기 위해 기본적인 삼각를 사용할 수 있도록 새로운 포인트가 떨어져 기존 점의에서 원주보다 작은 경우

- (void) positionCircles 
{ 

    //generate random Y value within a range 
    int fromNumberY = 500; 
    int toNumberY = 950; 
    int randomNumberY =(arc4random()%(toNumberY-fromNumberY+1))+fromNumberY; 

    //generate random Y value within a range 
    int fromNumberX = 0; 
    int toNumberX = 700; 
    int randomNumberX = (arc4random()%(toNumberX-fromNumberX+1))+fromNumberX; 

    //array to hold all the the CGPoints 
    positionsArray = [[NSMutableArray alloc] init]; 
    CGPoint circlePositionValue; 

    CGFloat radius = 70; 

    CGRect position = CGRectMake(randomNumberX,randomNumberY, radius, radius); 

    [self makeColors]; 



    // create a circle for each color in color array 
    for (int i = 0; i < [colors count];i++) 
    { 
     // generate new position before placing new cirlce 
     randomNumberX = (arc4random()%(toNumberX-fromNumberX+1))+fromNumberX; 
     randomNumberY = (arc4random()%(toNumberY-fromNumberY+1))+fromNumberY; 

     circlePositionValue = CGPointMake(position.origin.x, position.origin.y); 

     for (NSValue *value in positionsArray) { 


      BOOL xPointOk = (randomNumberX < value.CGPointValue.x - radius) || 
          (randomNumberX > value.CGPointValue.x + radius); 

      BOOL yPointOk = (randomNumberY < value.CGPointValue.y - radius) || 
          (randomNumberY > value.CGPointValue.y + radius); 


      NSLog(@"xPoint: %i - %f", randomNumberX , value.CGPointValue.x); 
      NSLog(@"xPoint ok? %@", [email protected]"yes":@"no"); 

      NSLog(@"yPoint: %i - %f", randomNumberY , value.CGPointValue.y); 
      NSLog(@"yPoint ok? %@", [email protected]"yes":@"no"); 
      NSLog(@"___"); 

     } 

     position.origin.x = randomNumberX; 
     position.origin.y = randomNumberY; 
     [positionsArray addObject:[NSValue valueWithCGPoint:circlePositionValue]]; 

     Circle *myCircle = [[Circle alloc] initWithFrame:position radius:radius color:[colors objectAtIndex:i]]; 
     myCircle.label.text = [NSString stringWithFormat:@"%i", i]; 
     [self.view addSubview:myCircle]; 

    } 

} 

답변

1

귀하의 테스트가 실패합니다.

BOOL far_enough_away = NO; 
CGPoint newpoint = CGZeroPoint; 
while(!far_enough_away) 
{ 
    newpoint = randomisation_thingy(); 
    far_enough_away = YES; 
    for(NSValue *existing in positionsArray) 
    { 
     CGPoint pointb = [existing pointValue]; 
     CGFloat deltay = pointb.y-newpoint.y; 
     CGFloat deltax = pointb.x-newpoint.x; 
     CGFloat distance = sqrt(pow(deltax,2) + pow(deltay,2)); 
     //fail if closer than desired radius 
     if(distance < circumference) 
     { 
      //sadness - try again 
      far_enough_away = NO; 
      break; 
     } 
    } 
} 
create_a_new_circle_at_point(newpoint); 

무한한 횟수의 재시도를 중지하기 위해 재 시도 할 횟수를 고려해야합니다.

+0

안녕하세요, 내 코드를 통합하려고했습니다. 그러나 나는 그것을 작동시키는 것처럼 보이지 않았다. 귀하의 방법이 확실히 효율적으로 할당되었음을 이해하는 동안. trig을 사용하지 않고도 작동시킬 수 있습니까? –

+0

의사 코드입니다. 그냥 복사/붙여 넣기를하지 마십시오. 프로그램에서 보는 것을 수정하십시오. 이 문제를 해결하려면 기본 삼각법을 사용해야합니다. –

+0

예, 이제 작동하게되었습니다. 감사! 비록 내가 몇 가지 기본적인 trig을 배울 필요가있다 :) –