1

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)**

enter image description here

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 ?

Chanchal Warde
  • 983
  • 5
  • 17
  • Possible duplicate of [Checking if two cubic Bézier curves intersect](http://stackoverflow.com/questions/4039229/checking-if-two-cubic-b%c3%a9zier-curves-intersect) – par Feb 01 '17 at 05:23
  • I checked that , it works fine but i gave 25 width to my line, it means there may be two line are not intersecting but there thickness are intersecting. I want to detect that perfectly .. – Chanchal Warde Feb 01 '17 at 05:25
  • I've retracted my close vote. – par Feb 01 '17 at 05:28
  • Any idea or clue to find answer? – Chanchal Warde Feb 01 '17 at 05:30
  • Possibly http://jazzros.blogspot.com/2011/03/projecting-point-on-bezier-curve.html?m=1 or https://hal.archives-ouvertes.fr/file/index/docid/518379/filename/Xiao-DiaoChen2007c.pdf ? – par Feb 01 '17 at 05:42

0 Answers0