Here is my requirement. Basically it is an eraser concept. I have polygon with n sides. (x,y) of all lines are stored in a model. To erase any part in the polygon, user can draw on the polygon over the lines and if the line comes completely under the coverage of the free hand drawing, the particular lines must be highlighted and later it could be erased.
Context drawing is used to draw both polygon and eraser. Based on the line data in model, the polygon is drawn. When user draws in eraser mode, it is stroked with larger line width in light blue colour. When the drawing has a complete coverage over any line in polygon, the line could be highlighted in dark blue indicating selection.
Problem is, I couldn't able to determine if the line has complete coverage over the eraser. Since I have only start and end point of a line, it gets difficult to determine it. What could be reliable way to identify the lines as user draws in eraser mode?
EDIT
Trying to provide answer for the questions in comment

For the first question, the bottom straight line has to be identified and deleted since it is covered completely. To answer second situation, none of the red lines will be selected/identified. To answer third, all lines that comes completely under the coverage of blue stroke must be selected - highlighted in green.

So, I guess you want something like this:
I'm turning the completely “erased” lines dashed instead of removing them entirely.
If your deployment target is at least iOS 16, then you can use the
lineSubtractingmethod ofCGPathto do the “heavy lifting”.Apple still hasn't provided real documentation of this method on the web site, but the header file describes it as follows:
So, here's the strategy:
CGPathfor one of your straight line segments.CGPathof the user's erase gesture.lineSubtractingon the straight line segment path, passing the stroked erase path, to get a path containing just the part of the straight line segment that is not covered by the eraser.lineSubtractingis empty, the straight line has been completely erased.Let's try it out. First, I'll write a model type to store both an original path and the part of that path that remains after erasing:
Let's add a couple of extra initializers, and a method that updates the
remainingpath by subtracting a (stroked) eraser path:I'll use the following function to turn an array of points into an array of
ErasablePaths:Here is the complete data model for the toy app:
To update the model as the user interacts with the app, I'll need methods to respond to the user starting, moving, and ending a touch, and a way to reset the data model:
We need a view that draws the erasable paths and the eraser path. I'll draw each original erasable path in green, and draw it dashed if it's been fully erased. I'll draw the remaining (unerased) part of each erasable path in red. And I'll draw the stroked eraser path in semitransparent purple.
In my
ContentView, I'll add aDragGestureon the drawing view, and also show a reset button:That's the code I used to generate the animation at the top of the answer. But I confess that it was a rigged demo. The
lineSubtractingmethod is a little buggy and I was careful to avoid triggering the bug. Here's the bug:If an
ErasablePathis a horizontal line segment, and the eraser path starts below that segment, thenlineSubtractingremoves the entire erasable path, even if the eraser path and the line segment have no overlap!To work around the bug, I insert the following
initmethod intoModel:The eraser path always starts above the erasable paths, so it no longer triggers the bug: