1625 字
8 分钟

贝塞尔曲线

2025-07-09
无标签
文章目录

What is Bezier Curve#

image.png
image.png

贝塞尔曲线是控制点按一定规则和比例插值得到的曲线。
他的几何意义下计算规则如下:

Given(p1=point1p2=point2distance=(p2p1)ratio=percentage100),new point=p1+distanceratioGiven \left( \begin{aligned} p_{1} &= point_{1} \\ p_{2} &= point_{2} \\ distance &= (p_{2}-p_{1}) \\ ratio &= \frac{percentage}{100} \end{aligned} \right), new \space point = p_{1}+distance\cdot ratio

Mathematics of Bezier Curve#

贝塞尔曲线是一种”参数化”函数(“parametric” function)。贝塞尔曲线是关于参数 tt 的多项式, 其中 t[0,1]t \in [0,1]
我们定义一个杨辉三角(Pascal’s triangle) 的打表:

linear=1+1squre=1+2+1cubic=1+3+3+1quartic=1+4+6+4+1\begin{aligned} \text{linear} &= 1+1 \\ \text{squre} &= 1+2+1 \\ \text{cubic} &= 1+3+3+1 \\ \text{quartic} &= 1+4+6+4+1 \end{aligned}

我们容易得到贝塞尔曲线在一阶, 二阶, 三阶的情况:

linear=(1t)+tsqure=(1t)2+2t(1t)+t2cubic=(1t)3+3t(1t)2+3t2(1t)+t3\begin{aligned} \text{linear} &= (1-t)+t \\ \text{squre} &= (1-t)^2+2t(1-t)+t^2 \\ \text{cubic} &= (1-t)^3+3t(1-t)^2+3t^2(1-t)+t^3 \\ \end{aligned}

进一步, 可以得到 Bezier 的通用公式:

Bezier(n,t)=nPascal(n,i)(1t)nitiBezier(n,t)= \sum^{n} \text{Pascal}(n,i)\cdot(1-t)^{n-i}\cdot t^i

Control Bezier Curvatures#

在上面的公式上, 再添加 Weight\text{Weight} 就是带有控制点的贝塞尔曲线公式:

Bezier(n,t)=nPascal(n,i)(1t)nitiWiBezier(n,t)= \sum^{n} \text{Pascal}(n,i)\cdot(1-t)^{n-i}\cdot t^i\cdot W_{i}

Weight\text{Weight} 是控制点的坐标值, W0W_{0} 是起始坐标, WnW_{n} 是终点坐标, 中间的 WiW_{i} 就是贝塞尔曲线的控制点。

Bezier Curvatures as Matrix Operations#

贝塞尔曲线能表示为矩阵形式, 特别地, 针对三次贝塞尔曲线, 可以表示为:

B(t)=[1tt2t3][1000330036301331][p1p2p3p4]B(t)=\begin{bmatrix} 1&t&t^2&t^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \cdot \begin{bmatrix} p_{1} \\ p_{2} \\ p_{3} \\ p_{4} \end{bmatrix}

de Casteljau’s Algorithm#

德卡斯特里奥算法 是计算伯恩斯坦形式的多项式或贝塞尔曲线递归方法。
算法计算步骤如下:

  1. 取曲线定义点间的所有线段, 对于 nn 阶的曲线, 就是 nn 条线段。

  2. 沿每条线段 tt 位置放置标记。

  3. 连接这些标记点, 形成 t1t-1 条线段。

  4. 在上述线段的 tt 位置再次放置标记。

  5. 连接这些标记点, 形成 t2t-2 条线段。

  6. 递归上述流程。

  7. 最终只剩下一条线段, 得到比率 tt 下的曲线位置。

image.png

Split Curve#

image.png

德卡斯特里奥算法可以实现贝塞尔曲线的分割。

Splite Curve using Matrices#

对于任意三次贝塞尔曲线

B(t)=[1tt2t3][1000330036301331][p1p2p3p4] B(t)=\begin{bmatrix} 1&t&t^2&t^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \cdot \begin{bmatrix} p_{1} \\ p_{2} \\ p_{3} \\ p_{4} \end{bmatrix}

可以在 t=zt=z 处分割为两条贝塞尔曲线。

那么引入 zz 的左曲线矩阵为:

B(t)=[1(zt)(zt)2(zt)3][1000330036301331][P1P2P3P4]=[1tt2t3][10000z0000z20000z3][1000330036301331][P1P2P3P4]\begin{align*} B(t) &= \begin{bmatrix} 1 & (z \cdot t) & (z \cdot t)^2 & (z \cdot t)^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \cdot \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \\ &= \begin{bmatrix} 1 & t & t^2 & t^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & z & 0 & 0 \\ 0 & 0 & z^2 & 0 \\ 0 & 0 & 0 & z^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \cdot \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \end{align*}

如果这个矩阵表达式侧能被表达为 [t Values][Bezier matrix][column matrix][\text{t Values}]\cdot[\text{Bezier matrix}]\cdot[\text{column matrix}] 的形式, 那么就可以得到 t=0t=0t=zt=z 的贝塞尔曲线矩阵。

简单起见, 推算过程以二次贝塞尔曲线矩阵为例:
首先, 我们定义三次贝塞尔矩阵为 MM

M=[1000330036301331]M= \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix}

定义 ZZ 矩阵

[10000z0000z20000z3]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & z & 0 & 0 \\ 0 & 0 & z^2 & 0 \\ 0 & 0 & 0 & z^3 \end{bmatrix}

那么

B(t)=[1tt2t3][10000z0000z20000z3][1000330036301331][P1P2P3P4]=[1tt2t3]ZM[1000330036301331]=[1tt2t3]MM1ZM[P1P2P3P4]\begin{align*} B(t)&= \begin{bmatrix} 1 & t & t^2 & t^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & z & 0 & 0 \\ 0 & 0 & z^2 & 0 \\ 0 & 0 & 0 & z^3 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \cdot \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \\ &= \begin{bmatrix} 1 & t & t^2 & t^3 \end{bmatrix} \cdot Z \cdot M \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ -3 & 3 & 0 & 0 \\ 3 & -6 & 3 & 0 \\ -1 & 3 & -3 & 1 \end{bmatrix} \\ &= \begin{bmatrix} 1 & t & t^2 & t^3 \end{bmatrix} \cdot M\cdot M^{-1}\cdot Z \cdot M \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \end{align*}

也就是说, 最后得到的点的仅需要乘以矩阵

Q=M1ZMQ=M^{-1}\cdot Z \cdot M

就得到了分割后矩阵的新的控制点。

另外, 这个算法是解析出来的, 针对我们常规的贝塞尔曲线解是唯一的(有理贝塞尔曲线会添加控制点影响度的参数, 解控制点的位置就不唯一)。

最后得到左曲线:

[1000(z1)z00(z1)22(z1)zz20(z1)33(z1)2z3(z1)z2z3][P1P2P3P4]=[P1zP2(z1)P1z2P32z(z1)P2+(z1)2P1z3P43z2(z1)P3+3z(z1)2P2(z1)3P1] \begin{align*} &\begin{bmatrix} 1 & 0 & 0 & 0 \\ -(z-1) & z & 0 & 0 \\ (z-1)^2 & -2 \cdot (z-1) \cdot z & z^2 & 0 \\ -(z-1)^3 & 3 \cdot (z-1)^2 \cdot z & -3 \cdot (z-1) \cdot z^2 & z^3 \end{bmatrix} \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \\= &\begin{bmatrix} P_1 \\ z \cdot P_2 - (z-1) \cdot P_1 \\ z^2 \cdot P_3 - 2 \cdot z \cdot (z-1) \cdot P_2 + (z-1)^2 \cdot P_1 \\ z^3 \cdot P_4 - 3 \cdot z^2 \cdot (z-1) \cdot P_3 + 3 \cdot z \cdot (z-1)^2 \cdot P_2 - (z-1)^3 \cdot P_1 \end{bmatrix} \end{align*}

得到右曲线:

[(z1)33(z1)2z3(z1)z2z30(z1)22(z1)zz200(z1)z0001][P1P2P3P4]=[z3P43z2(z1)P3+3z(z1)2P2(z1)3P1z2P42z(z1)P3+(z1)2P2zP4(z1)P3P4]\begin{align*} &\begin{bmatrix} -(z-1)^3 & 3 \cdot (z-1)^2 \cdot z & -3 \cdot (z-1) \cdot z^2 & z^3 \\ 0 & (z-1)^2 & -2 \cdot (z-1) \cdot z & z^2 \\ 0 & 0 & -(z-1) & z \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} P_1 \\ P_2 \\ P_3 \\ P_4 \end{bmatrix} \\= &\begin{bmatrix} z^3 \cdot P_4 - 3 \cdot z^2 \cdot (z-1) \cdot P_3 + 3 \cdot z \cdot (z-1)^2 \cdot P_2 - (z-1)^3 \cdot P_1 \\ z^2 \cdot P_4 - 2 \cdot z \cdot (z-1) \cdot P_3 + (z-1)^2 \cdot P_2 \\ z \cdot P_4 - (z-1) \cdot P_3 \\ P_4 \end{bmatrix} \end{align*}

Possibility of Curve Segmentation#

我们已经得到了贝塞尔曲线的分割的解析解, 现在需要考察的是, 在 1/31/32/32/3 处是否一定能找到对应的解析解。
也就是说, 矩阵方程

[x1zx2(z1)x1z2x32z(z1)x2+(z1)2x1x4=z3x43z2(z1)x3+3z(z1)2x2(z1)3x1]=[x123x1+13x413x1+23x4x4] \begin{align*} &\begin{bmatrix} x_1 \\ z \cdot x_2 - (z-1) \cdot x_1 \\ z^2 \cdot x_3 - 2 \cdot z \cdot (z-1) \cdot x_2 + (z-1)^2 \cdot x_1 \\ x'_{4}=z^3 \cdot x_4 - 3 \cdot z^2 \cdot (z-1) \cdot x_3 + 3 \cdot z \cdot (z-1)^2 \cdot x_2 - (z-1)^3 \cdot x_1 \end{bmatrix} \\= &\begin{bmatrix} x_1 \\ \frac{2}{3}x_{1}+\frac{1}{3}x'_{4} \\ \frac{1}{3}x_{1}+\frac{2}{3}x'_{4} \\ x'_{4} \end{bmatrix} \end{align*}

尝试展开式子, 显然, 这个式子很容易看出是无解的。
所以得出结论, 用曲线分割的方式去得到 1/31/32/32/3 的固定三次贝塞尔贝塞尔表达任意三次贝塞尔曲线是没有解析解的。

Reference#

A Primer on Bézier Curves
知乎 - 贝塞尔曲线
贝塞尔曲线 - 维基百科,自由的百科全书
De Casteljau’s algorithm - Wikipedia

原附件:preview-9781483296999_A23889377.pdf(原仓库未包含此文件)。