I am using following code to draw line when user drag finger on screen, when user touches I draw a line on from start to end point.
I have stored all bezier paths in array, now what I want to achieve is if user draw line on screen it should not overlap any other line. For this I have used contains
method but it gives only points if it is in bezier path. The problem is my line width is 20 and it does not detect the points line which covers by line width. I can detect 1 the red on intersection but not second the green on, because it actually line with seems to overlap each other not bezier path points. Is there is way to find exact overlapping.
func drawLine(from : CGPoint , to : CGPoint)
{
autoreleasepool {
drawImage.image = UIImage(named:"abc")
UIGraphicsBeginImageContextWithOptions(drawImage.bounds.size, false, 0)
UIColor.init(colorLiteralRed: 181/255, green: 82/255, blue: 26/255, alpha: 0.60).set()
drawImage.image?.draw(in: view.bounds)
let context = UIGraphicsGetCurrentContext()
lassoBezier = UIBezierPath()
lassoBezier.move(to: from)
lassoBezier.addLine(to: to)
lassoBezier.lineWidth = 20
lassoBezier.stroke()
context?.addPath(lassoBezier.cgPath)
drawImage.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
**lassoBezier.contains(toPoint)**
I am using this to check overlap but sometimes it don't work. Sometimes it say overlap when its not and some time it say not overlapping but its overlapping.
func checkIfOverlapp(pointsTo: CGPoint) {
for bezier in arrRecentBezierPaths
{
let strokedPath = bezier.cgPath.copy(strokingWithWidth: K_Tap_Width + 10 , lineCap: .square , lineJoin: .round, miterLimit: 1, transform: .identity)
let pointIsNearPath = strokedPath.contains(pointsTo)
if pointIsNearPath == true
{
isOverlap = true
}
}
}
Any Help ?