0

I've a QuadCurve and Line drawn using UIBezierPath in my custom view class. How can I get their intersection point as CGPoint?

Bezier Paths

For QuadCurve:

let path = UIBezierPath()
path.lineWidth = 3.0
path.move(to: CGPoint(x: 0, y: self.frame.size.height))
path.addQuadCurve(to: CGPoint(x: self.frame.size.width, y: 0), controlPoint: CGPoint(x: self.frame.size.width-self.frame.size.width/3, y: self.frame.size.height))

For Line:

let path2 = UIBezierPath()
path2.lineWidth = 3.0
path2.move(to: CGPoint(x: 250, y: 0))
path2.addLine(to: CGPoint(x: 250, y: self.frame.size.height))
bijesh
  • 55
  • 8
  • check this https://stackoverflow.com/questions/6279999/finding-a-dot-on-a-circle-by-degree – canister_exister Dec 21 '18 at 19:57
  • @canister_exister, thank you for your comment but my requirement is of a curve. I tried to do the same with an arc but I'm not getting an expected output – bijesh Dec 24 '18 at 05:22

1 Answers1

1

If your line is always vertical, calculations are quite simple: x-coordinate is known, so you task is to find y-coordinate. Quadratic Bezier curve has parametric representation:

P(t) = P0*(1-t)^2 + 2*P1*(1-t)*t + P2*t^2 = 
       t^2 * (P0 - 2*P1 + P2) + t * (-2*P0 + 2*P1)  + P0

where P0, P1, P2 are starting, control and ending points.

So you have to solve quadratic equation

t^2 * (P0.X - 2*P1.X + P2.X) + t * (-2*P0.X + 2*P1.X)  + (P0.X - LineX) = 0

for unknown t, get root in range 0..1, and apply t value to the similar expression for Y-coordinate

Y = P0.Y*(1-t)^2 + 2*P1.Y*(1-t)*t + P2.Y*t^2

For arbitrary line make equation system for line parametric representation and curve, and solve that system

MBo
  • 77,366
  • 5
  • 53
  • 86