I'm trying to detecting wether a given point is inside a closed SVG path in Objective-C. I'm having trouble figuring out how to do the math.
I have a path's coordinates and I'd like to determine wether a random point is inside or outside the path.
Here's an example of a path's coordinates:
"M673 460 c2 0 4 -1 5 -2 1 -1 2 -2 2 -4 0 -2 0 -3 0 -3 0 0 -3 1 -5 1 -3 1 -5 2 -5 3 0 1 0 3 0 4 1 0 2 1 3 1z:"
I'm aware of the CoreGraphics's containsPoint:
method, but I'd like to avoid using this method.
How can I do this by writing my own method?
EDIT:
I'm trying to avoid containsPoint:
because the function seems to crash on some coordinates / paths when using it. It looks quite random when it crashes and when it doesn't.
Here are some examples of paths where containsPoint:
makes the app crash:
"M661 446 c1 -1 3 -1 4 -1 1 -1 2 -2 2 -4 0 -2 0 -2 -2 -2 0 1 -2 1 -3 1 -2 0 -3 1 -3 2 0 1 0 2 0 3 0 0 1 1 2 1z"
"M535 460 c0 0 1 -1 1 -2 1 -2 0 -3 -1 -3 0 0 -1 0 -2 1 0 1 0 2 0 3 1 0 1 1 2 1z"
Xcode breaks with and EXC_BAD_ACCESS
in the assembly by the following two functions: get_y_inflections
and get_cubic_coefficients
.
EDIT 2:
I posted a new question about the containsPoint:
problem.
So, as i needed a Polygon/Interpolation from a Curved Path myself, i might have a solution for your problem as well:
The two 'Public' Functions:
CGPathRef CGPathCreatePolygonPath(CGPathRef path, int quality);
Creates an Path with only Line-Elements, quality is the number of segments a curve is split into
BOOL CGPathContainsPointInterpolated(CGPathRef path, const CGAffineTransform *m, CGPoint point, bool eoFill, int quality);
Does CGPathContainsPoint and Interpolation in one step, quality is again the number of segments a curve is split into.
Here is the implementation.