The only completely accurate way I found for this is to scroll through each point, something like this (I have not tested this exact code):
for (NSValue *rectValue in rects) { CGRect rect = rectValue.CGRectValue; // Set the initial point values to the top left of the current rect CGFloat x = CGRectGetMinX(rect); CGFloat y = CGRectGetMinY(rect); BOOL intersects = NO; BOOL morePoints = YES; // Loop until there are no more points to check in this rect while (morePoints) { CGPoint point = CGPointMake(x, y); if (CGPathContainsPoint(path, nil, point, NO)) { intersects = YES; break; } // Adjust the x and y values to check all points if (x < CGRectGetMaxX(rect)) { x++; } else if (y < CGRectGetMaxY(rect)) { x = CGRectGetMinX(rect); y++; } else { morePoints = NO; } } if (intersects) { // The path intersects the current rect } else { // The path does not intersect the current rect } }
But this is the most inefficient way.
You can optimize this solution through an asynchronous loop:
[rects enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSValue * _Nonnull rectValue, NSUInteger idx, BOOL * _Nonnull stop) { CGRect rect = rectValue.CGRectValue; ... }];
I only needed to know if the path crossed the edge of the rectangle, so I did something similar to this:
for (NSValue *rectValue in rects) { CGRect rect = rectValue.CGRectValue; // Set the initial point values to the top left of the current rect CGFloat x = CGRectGetMinX(rect); CGFloat y = CGRectGetMinY(rect); BOOL intersects = NO; // top edge for (; x < CGRectGetMaxX(rect); x++) { CGPoint point = CGPointMake(x, y); if (CGPathContainsPoint(path, nil, point, NO)) { intersects = YES; break; } } if (intersects) { // The path intersects the current rect on the top edge } // right edge for (; y < CGRectGetMaxY(rect); y++) { CGPoint point = CGPointMake(x, y); if (CGPathContainsPoint(path, nil, point, NO)) { intersects = YES; break; } } if (intersects) { // The path intersects the current rect on the right edge } // bottom edge x = CGRectGetMinX(rect); for (; x < CGRectGetMaxX(rect); x++) { CGPoint point = CGPointMake(x, y); if (CGPathContainsPoint(path, nil, point, NO)) { intersects = YES; break; } } if (intersects) { // The path intersects the current rect on the bottom edge } // left edge x = CGRectGetMinX(rect); y = CGRectGetMinY(rect); for (; y < CGRectGetMaxY(rect); y++) { CGPoint point = CGPointMake(x, y); if (CGPathContainsPoint(path, nil, point, NO)) { intersects = YES; break; } } if (intersects) { // The path intersects the current rect on the left edge } }
source share