数学图形(1.47)贝塞尔(Bézier)曲线

      贝塞尔曲线又称贝兹曲线或贝济埃曲线,是由法国数学家Pierre Bézier所发现,由此为计算机矢量图形学奠定了基础。它的主要意义在于无论是直线或曲线都能在数学上予以描述。

      上一节讲的是高次方程曲线,其实贝塞尔曲线就是高次函数曲线.研究贝塞尔曲线的人最初是按照已知曲线参数方程来确定四个点的思路设计出这种矢量曲线绘制法。涕淌为了向大家 介绍贝塞尔曲线的公式,也故意把问题的已知和所求颠倒了一下位置:如果已知一条曲线的参数方程,系数都已知,并且两个方程里都含有一个参数t,它的值介于 0、1之间,表现形式如下所示:

      x(t) = ax * t ^ 3 + bx * t ^ 2 + cx * t + x0
      y(t) = ay * t ^ 3 + by * t ^ 2 + cy * t + y0

      由N个顶点控制的贝塞尔曲线,是N-1次的函数方程构成.

二次方贝塞尔曲线

      二次方贝塞尔曲线的路径由给定点P0P1P2的函数Bt):

      mathbf{B}(t) = (1 - t)^{2}mathbf{P}_0 + 2t(1 - t)mathbf{P}_1 + t^{2}mathbf{P}_2 mbox{ , } t in [0,1]

三次方贝塞尔曲线

      P0P1P2P3四个点在平面或在三维空间中定义了三次方贝塞尔曲线。曲线起始于P0走向P1,并从P2的方向来到P3。一般不会经过P1P2;这两个点只是在那里提供方向资讯。P0P1之间的间距,决定了曲线在转而趋进P3之前,走向P2方向的“长度有多长”。

      mathbf{B}(t)=mathbf{P}_0(1-t)^3+3mathbf{P}_1t(1-t)^2+3mathbf{P}_2t^2(1-t)+mathbf{P}_3t^3 mbox{ , } t in [0,1]

n阶贝塞尔曲线可如下推断:

      给定点P0P1、…、Pn,其贝塞尔曲线即

      mathbf{B}(t)=sum_{i=0}^n {nchoose i}mathbf{P}_i(1-t)^{n-i}t^i ={nchoose 0}mathbf{P}_0(1-t)^nt^{0}+{nchoose 1}mathbf{P}_1(1-t)^{n-1}t^{1}+cdots+{nchoose n-1}mathbf{P}_{n-1}(1-t)^{1}t^{n-1}+{nchoose n}mathbf{P}_n(1-t)^{0}t^n mbox{ , } t in [0,1]

相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815

如下是我写的贝塞尔曲线的脚本代码与截图,代码中的控制顶点坐标为随机数生成.

二次方贝塞尔曲线:

vertices = 360

t = from 0 to 1

ax = rand2(-10, 10)
ay = rand2(-10, 10)
bx = rand2(-10, 10)
by = rand2(-10, 10)
cx = rand2(-10, 10)
cy = rand2(-10, 10)

a1 = (1-t)*(1-t)
a2 = 3*t*(1-t)
a3 = t*t

x = a1*ax+a2*bx+a3*cx 
y = a1*ay+a2*by+a3*cy

三次方贝塞尔曲线:

vertices = 360

t = from 0 to 1

ax = rand2(-10, 10)
ay = rand2(-10, 10)
bx = rand2(-10, 10)
by = rand2(-10, 10)
cx = rand2(-10, 10)
cy = rand2(-10, 10)
dx = rand2(-10, 10)
dy = rand2(-10, 10)

a1 = pow((1-t),3) 
a2 = pow((1-t),2)*3*t  
a3 = 3*t*t*(1-t)
a4 = t*t*t

x = a1*ax+a2*bx+a3*cx+a4*dx;  
y = a1*ay+a2*by+a3*cy+a4*dy; 

四次方贝塞尔曲线:

vertices = 360

t = from 0 to 1

ax = rand2(-10, 10)
ay = rand2(-10, 10)
bx = rand2(-10, 10)
by = rand2(-10, 10)
cx = rand2(-10, 10)
cy = rand2(-10, 10)
dx = rand2(-10, 10)
dy = rand2(-10, 10)
ex = rand2(-10, 10)
ey = rand2(-10, 10)

t2 = pow(t,2)
t3 = pow(t,3)
t4 = pow(t,4)

w = 1-t
w2 = pow(w,2)
w3 = pow(w,3)
w4 = pow(w,4)

a1 = w4 
a2 = 4*w3*t  
a3 = 6*w2*t2
a4 = 4*w*t3
a5 = t4

x = a1*ax+a2*bx+a3*cx+a4*dx+a5*ex;  
y = a1*ay+a2*by+a3*cy+a4*dy+a5*ex; 

五次方贝塞尔曲线:

vertices = 360

t = from 0 to 1

ax = rand2(-10, 10)
ay = rand2(-10, 10)
bx = rand2(-10, 10)
by = rand2(-10, 10)
cx = rand2(-10, 10)
cy = rand2(-10, 10)
dx = rand2(-10, 10)
dy = rand2(-10, 10)
ex = rand2(-10, 10)
ey = rand2(-10, 10)
fx = rand2(-10, 10)
fy = rand2(-10, 10)

t2 = pow(t,2)
t3 = pow(t,3)
t4 = pow(t,4)
t5 = pow(t,5)

w = 1-t
w2 = pow(w,2)
w3 = pow(w,3)
w4 = pow(w,4)
w5 = pow(w,5)

a1 = w5 
a2 = 5*w4*t
a3 = 10*w3*t2
a4 = 10*w2*t3
a5 = 5*w*t4
a6 = t5

x = a1*ax+a2*bx+a3*cx+a4*dx+a5*ex+a6*fx;  
y = a1*ay+a2*by+a3*cy+a4*dy+a5*ex+a6*fx; 

原文地址:https://www.cnblogs.com/WhyEngine/p/3988293.html