I've a QuadCurve and Line drawn using UIBezierPath in my custom view class. How can I get their intersection point as CGPoint?
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))
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