數學圖形(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+3\mathbf{P}_1t(1-t)^2+3\mathbf{P}_2t^2(1-t)+\mathbf{P}_3t^3 \mbox{ , } t \in [0,1]

n階貝塞爾曲線可如下推斷:

      給定點P0P1、…、Pn,其貝塞爾曲線即

      \mathbf{B}(t)=\sum_{i=0}^n {n\choose i}\mathbf{P}_i(1-t)^{n-i}t^i ={n\choose 0}\mathbf{P}_0(1-t)^nt^{0}+{n\choose 1}\mathbf{P}_1(1-t)^{n-1}t^{1}+\cdots+{n\choose n-1}\mathbf{P}_{n-1}(1-t)^{1}t^{n-1}+{n\choose 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; 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM