简体   繁体   English

将新的 CGPoint 值与数组中的 CGPoint 值进行比较

[英]compare new CGPoint value to a CGPoint value in Array

I'm generating a number of circles with random positions.我正在生成许多具有随机位置的圆圈。 what I want to prevent is the circles from overlapping each other.我要防止的是圆圈相互重叠。 so I want to compare the newly generated CGPoint value to the ones that are in the array and have it generate a new value if the value finds itself in the radius area of one of the CGPoints.所以我想将新生成的 CGPoint 值与数组中的值进行比较,如果该值位于其中一个 CGPoint 的半径区域中,则让它生成一个新值。

I got most of it except for the overlapping thing.除了重叠的东西,我得到了大部分。 I've commented the part out of something I tried, but that doesn't really work(flawed logic).我已经对我尝试过的部分进行了评论,但这并没有真正起作用(有缺陷的逻辑)。 I'm not sure how to iterate through the individual values of the CGPoint and comparing them.我不确定如何遍历 CGPoint 的各个值并比较它们。

UPDATE: I've change the code a bit more and added a check to see whether the CGPoint will overlap or not.更新:我对代码做了更多更改,并添加了一个检查以查看 CGPoint 是否会重叠。 Problem is whenever I want to try something like问题是每当我想尝试类似

while (!xPointOk || !yPointOk) {

// generate new values

}

I keep falling in an infinite loop.我一直陷入无限循环。

- (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];

    }

}

Your test should fail when the new point is less than the circumference away from any of the existing points so you can use basic trig to work it out.当新点小于距任何现有点的圆周时,您的测试应该会失败,因此您可以使用基本三角来解决它。

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

Other things you need to consider are how many times to retry to stop an infinite number of retries.您需要考虑的其他事情是重试多少次以停止无限次重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM