简体   繁体   中英

How to find intersection point (CGPoint) between QuadCurve and Line UIBezierPaths in swift?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM