问题
What is the algorithm to convert a quadratic bezier (with 3 points) to a cubic one (with 4 points)?
回答1:
From http://fontforge.sourceforge.net/bezier.html:
Any quadratic spline can be expressed as a cubic (where the cubic term is zero). The end points of the cubic will be the same as the quadratic's.
CP0 = QP0
CP3 = QP2The two control points for the cubic are:
CP1 = QP0 + 2/3 *(QP1-QP0)
CP2 = QP2 + 2/3 *(QP1-QP2)...There is a slight error introduced due to rounding, but it is unlikely to be noticeable.
回答2:
For reference, I implemented addQuadCurve for NSBezierPath (macOS Swift 4) based on Owen's answer above.
extension NSBezierPath {
public func addQuadCurve(to qp2: CGPoint, controlPoint qp1: CGPoint) {
let qp0 = self.currentPoint
self.curve(to: qp2,
controlPoint1: qp0 + (2.0/3.0)*(qp1 - qp0),
controlPoint2: qp2 + (2.0/3.0)*(qp1 - qp2))
}
}
extension CGPoint {
// Vector math
public static func +(left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
public static func -(left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x - right.x, y: left.y - right.y)
}
public static func *(left: CGFloat, right: CGPoint) -> CGPoint {
return CGPoint(x: left * right.x, y: left * right.y)
}
}
来源:https://stackoverflow.com/questions/3162645/convert-a-quadratic-bezier-to-a-cubic-one