How to determine if NSBezierPaths intersect in Cocoa?
cocoa, nsbezierpath, objective-c
Solution
If you have 2 bezier path rectangles and know each of their frames, then you can use `NSIntersectsRect()`:
NSRect rect1 = NSMakeRect(20.0, 150.0, 300.0, 100.0);
NSRect rect2 = NSMakeRect(100.0, 100.0, 100.0, 200.0);
[[NSColor redColor] set];
[NSBezierPath strokeRect:rect1];
[NSBezierPath strokeRect:rect2];
BOOL intersects = NSIntersectsRect(rect1, rect2);
NSLog(@"intersects == %@", (intersects ? @"YES" : @"NO"));
Produces:
In this case, it would log `intersects == YES`.
Problem
I'm having a hard time figuring out how to determine the intersection of two NSBezierPath closed objects in cocoa. I did some research online and couldn't find the answer so far. Here is what I have. I need to write some kind of a method that would return true in all of these cases. What I was thinking so far is to flatten the rectangle by using bezierPathByFlatteningPath and then take each element (as a line segment) using elementAtIndex: associatedPoints: to go through every point in it and checking if the second object (rectangle or ellipse) contains that point (using containsPoint:). However, I don't know how to go through all the points of a segment... If anyone has any hint or idea that might help I would really appreciate it!