Subtracting UIBezierPath from another UIBezierPath

I am creating an application that allows the user to draw on the screen with a finger of different colors. Drawings are drawn using UIBezierPaths, but I need an eraser. I had an eraser that was just expensive with a background image as a color, but this method causes memory problems. I would like to remove points from any path that is drawn when the eraser is selected.

Unfortunately, UIBezierPath does not have a subtraction function, so I want to make my own. Therefore, if an eraser is selected, it will look at all the points that should be deleted and see if any of the existing paths contain these points, then divide the path, leaving an empty space. But he should be able to see how many points in the line to delete, not to do this one at a time. In theory, this makes sense, but it's hard for me to start working on an implementation.

Does anyone have any directions to set me on the right path?

+4
source share
1 answer

At first glance, it seems that you can do hit detection on UIBezierPath just by using containsPoint: This works great if you want to determine if this point is contained in the UIBezierPath pad, but it does not work to determine if only the point UIBezierPath intersects the point. Detecting whether a given point is in the UIBezierPath beat can be performed as described in the โ€œDoing Hit-Detection on Pathโ€ section at the bottom of this page . In fact, the sample code they give can be used anyway. The basic idea is that you should use the Core Graphics method CGContextPathContainsPoint .

Depending on how big the eraser brush is, you probably want to check out several different points on the edge of the brush circle to see if they intersect the curve, and you probably have to go through your UIBezierPaths until you get hit. You should be able to optimize your search using the bounds UIBezierPath.

After you find that the point crosses the UIBezierPath, you must perform the actual path separation. This seems to be a good algorithm in this post . The basic idea is to use the De Castellau algorithm to perform curve splitting. There are various implementations of the algorithm that you should find with a quick search, including in C ++.

+3
source

All Articles