Search code examples
iosnspredicatecgrectcgpoint

NSPredicate on an NSArray of CGPoints


I have an NSArray of CGPoints.

I actually want to get the minimal CGRect that contains all the CGPoints in that array. Is there already any iOS function to do this?

Otherwise I will have to manually fetch minX, maxX, minY, maxY, and construct my CGRect this way:

CGrectMake(minX, minY, maxX-minX, maxY-minY);

For this I need to filter the NSArray with an NSPredicate.

(I know, I could filter the array manually, but come on!...)


Solution

  • There is no public API that does what you're asking. You'll have to write it yourself.

    It wouldn't be appropriate for NSPredicate anyway. A predicate is for filtering values, not for constructing new values.

    CGRect boundsOfPointArray(NSArray *array) {
        CGRect bounds = CGRectNull;
        for (NSValue *wrapper in array) {
            CGRect r = (CGRect){ wrapper.CGPointValue, CGSizeZero };
            bounds = CGRectUnion(bounds, r);
        }
    }