【问题标题】:How to find intersection point (CGPoint) between QuadCurve and Line UIBezierPaths in swift?如何快速找到 QuadCurve 和 Line UIBezierPaths 之间的交点(CGPoint)?
【发布时间】:2018-12-21 18:45:26
【问题描述】:

我在自定义视图类中使用 UIBezierPath 绘制了 QuadCurve 和 Line。我怎样才能得到他们的交点作为CGPoint?

对于 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))

对于线路:

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

【问题讨论】:

标签: ios swift intersection uibezierpath bezier


【解决方案1】:

如果你的线总是垂直的,计算就很简单:x 坐标是已知的,所以你的任务是找到 y 坐标。二次贝塞尔曲线有参数表示:

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

P0, P1, P2 是起点、控制点和终点。

所以你必须解二次方程

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

对于未知的t,在0..1 范围内求根,并将t 值应用于Y 坐标的类似表达式

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

对于任意线,建立线参数表示和曲线的方程组,并求解该系统

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    相关资源
    最近更新 更多