暫時放棄了用opengl繪圖。因為數據結構略復雜,暫時投奔了MATLAB的懷抱,下面利用MATLAB改寫繪制Bezier曲線的代碼
直接上代碼,首先是直接利用基函數的繪制
function bezier( vertices )%BEZIER 繪制Bezier曲線Dim=size(vertices,1);%二位或者三維空間NumPoint=size(vertices,2)-1;%點的個數t=0:0.001:1;x=[];y=[];z=[];if Dim==2 x=(1-t).^(NumPoint)*vertices(1,1); y=(1-t).^(NumPoint)*vertices(2,1); for j=1:NumPoint w=factorial(NumPoint)/(factorial(j)*factorial(NumPoint-j))*(1-t).^(NumPoint-j).*t.^(j); x=x+w*vertices(1,j+1);y=y+w*vertices(2,j+1); endplot(vertices(1,:),vertices(2,:),'b');hold on;grid on;axis tight; xlabel('X');ylabel('Y');plot(x,y,'r');endif Dim==3 x=(1-t).^(NumPoint)*vertices(1,1); y=(1-t).^(NumPoint)*vertices(2,1); z=(1-t).^(NumPoint)*vertices(3,1); for j=1:NumPoint w=factorial(NumPoint)/(factorial(j)*factorial(NumPoint-j))*(1-t).^(NumPoint-j).*t.^(j); x=x+w*vertices(1,j+1);y=y+w*vertices(2,j+1);z=z+w*vertices(3,j+1); endplot3(vertices(1,:),vertices(2,:),vertices(3,:),'b');hold on;grid on;axis tight; %axis([0.5,1.5,0.5,1.5,0,0.7]);xlabel('X');ylabel('Y');zlabel('Z');plot3(x,y,z,'r');view(3);endend不得不佩服MATLAB的強大。。下面展示兩個例子
vertices=[0.8 0.8 0;1.4 0.6 0;1.5 0.7 0.5;1.5 1.5 .5;0.6 1.4 0;1.5 1.5 0]';bezier( vertices )再去掉代碼中關于坐標軸的注釋,效果如下

如果是二維的,點換做
vertices = [0 3; 3 7;7 2; 9 6;13 3]';結果為
新聞熱點
疑難解答