Reputation: 1081
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!...)
Upvotes: 0
Views: 480
Reputation: 5175
This question is very unclear. Do you mean you want to create a minimal CGRect that contains all the points you have in an array? If so, that is pretty simple, just iterate over the array of points keeping track of the minX, maxX, minY and maxY. eg
CGFloat minX = CGFLOAT_MAX;
CGFloat minY = CGFLOAT_MAX;
CGFloat maxX = -CGFLOAT_MAX;
CGFloat maxY = -CGFLOAT_MAX;
for (NSValue *v in pointsArray) {
CGPoint p = [v CGPointValue];
minX = fminf(minX, p.x);
minY = fminf(minY, p.y);
maxX = fmaxf(maxX, p.x);
maxY = fmaxf(maxY, p.y);
}
CGRect r = CGRectMake(minX, minY, maxX - minX, maxY - minY);
Upvotes: 1
Reputation: 385970
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);
}
}
Upvotes: 2