简体   繁体   English

如何快速找到QuadCurve和Line UIBezierPaths之间的交点(CGPoint)?

[英]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. 我在自定义视图类中使用UIBezierPath绘制了QuadCurve和Line。 How can I get their intersection point as CGPoint? 如何将他们的交点设为CGPoint?

贝塞尔曲线

For QuadCurve: 对于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. 如果您的线始终是垂直的,则计算非常简单:x坐标已知,因此您的任务是找到y坐标。 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. 其中P0, P1, P2是起点,控制点和终点。

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 对于未知的t ,获得0..1范围内的根,并将t值应用于Y坐标的类似表达式

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 对于任意线,建立用于线参数表示和曲线的方程组,并求解该系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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