8

現在タッチされているポイントの色を取得しようとしているアプリケーションを作成しています。このポイントが黒でない場合、反復/再帰が実行され、黒以外のすべてのポイントが配列に格納されます。私は反復のためにこの関数を使用しています:

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    if (canDoPositiveX == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX+1 :positiveY :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = NO;
        }
    }

    if (canDoPositiveY == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 0"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY+1 :negativeX :negativeY];
        }
        else
        {
            canDoPositiveY = NO;
        }
    }

    if (canDoNegativeX == YES)
    {
        //Checking in negative
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX-1 :negativeY];
        }
        else
        {
            canDoNegativeX = NO;
        }
    }

    if (canDoNegativeY == YES)
    {
        //Checking in negative        
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            negativeY -= 1;
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX :negativeY-1];
        }
        else
        {
            canDoNegativeY = NO;
        }
    }

    NSLog(@"%u",[array count]);

}

今、私は配列にあるそれらのポイントの色を変更していますが、すべてのピクセルではなく、ポイントの直線のみに色を付けています。どんな助けでも本当に感謝します。ありがとう!

4

3 に答える 3

2

これを試して。実際にテストすることはできませんが、動作するはずです。

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    // Check your current point 
    if ([[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
    {
        // reached the dead end (break recursion)
        return;
    }

    CGPoint point = CGPointMake(positiveX, positiveY);
    [array addObject:[NSValue valueWithCGPoint:point]];

    BOOL canDoPositiveX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX+1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoPositiveY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY+1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX-1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY-1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];

    if (canDoPositiveX == YES)
    {
        [self function:positiveX+1 :positiveY :negativeX :negativeY];
    }

    if (canDoPositiveY == YES)
    {
        [self function:positiveX :positiveY+1 :negativeX :negativeY];
    }

    if (canDoNegativeX == YES)
    {
        [self function:positiveX :positiveY :negativeX-1 :negativeY];
    }

    if (canDoNegativeY == YES)
    {
        [self function:positiveX :positiveY :negativeX :negativeY-1];
    }

    NSLog(@"%u",[array count]);
}

また、これらの変数をクラスから削除します canDoPositiveX、canDoPositiveY、canDoNegativeX、canDoNegativeY

于 2013-03-26T09:42:53.033 に答える