1

ランダムな位置で多数の円を生成しています。私が防ぎたいのは、円が互いに重ならないようにすることです。そのため、新しく生成された CGPoint 値を配列内の値と比較し、値が CGPoints のいずれかの半径領域にある場合は新しい値を生成したいと考えています。

重複したものを除いて、ほとんどのものを手に入れました。私が試したことの一部をコメントアウトしましたが、それは実際には機能しません(論理の欠陥)。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? %@", xPointOk?@"yes":@"no");

            NSLog(@"yPoint: %i - %f", randomNumberY , value.CGPointValue.y);
            NSLog(@"yPoint ok? %@", yPointOk?@"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];

    }

}
4

1 に答える 1

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

他に考慮する必要があるのは、無限回の再試行を停止するために何回再試行するかです。

于 2012-04-08T11:42:40.590 に答える