views:

731

answers:

2

Hi all,

Is there a way to find out whether two CGPathRefs are intersected or not. In my case all the CGPaths are having closePath.

For example, I am having two paths. One path is the rectangle which is rotated with some angle and the other path is curved path. Two paths origin will be changing frequently. At some point they may intersect. I want to know when they are intersected. Please let me know if you have any solution.

Thanks in advance

A: 

1) There isn't any CGPath API to do this. But, you can do the math to figure it out. Take a look at this wikipedia article on Bezier curves to see how the curves in CGPath are implemented.

2) This is going to be slow on the iPhone I would expect but you could fill both paths into a buffer in difference colors (say, red and blue, with alpha=0.5) and then iterate through the buffer to find any pixels that occur at intersections. This will be extremely slow.

Steven Canfield
A: 

Just draw them both at 50% opacity into your own memory buffer (created with CGBitmapContextCreate and then set to black), checking for pixel values > 128.
If you're on the iPhone, you'll probably have to use RGBA as it doesn't support that
many bitmap types, so your per-pixel processing code would look something like this:

if(*p > 128) return true; // curves intersect
else p+= 4; // keep looking

Let the resolution of the rasterised versions be your precision and choose the precision to suit your performance needs.

Rhythmic Fistman
Thanks for the solution. But, I am not that good at Core Graphics. How can we retrieve and check for non-white pixels.