MATLAB-立體繪圖命令詳解

基本XYZ立體繪圖命令

在科學目視表示(Scientific visualization)中,三度空間的立體圖是一個非常重要的技巧。本章將介紹MATLAB基本XYZ三度空間的各項繪圖命令。
mesh和plot是三度空間立體繪圖的基本命令,mesh可畫出立體網狀圖,plot則可畫出立體曲面圖,兩者產生的圖形都會依高度而有不同顏色。

下列命令可畫出由函數 形成的立體網狀圖:

x=linspace(-2, 2, 25); % 在x軸上取25點
y=linspace(-2, 2, 25); % 在y軸上取25點
[xx,yy]=meshgrid(x, y); % xx和yy都是21x21的矩陣
zz=xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz也是21x21的矩陣
mesh(xx, yy, zz); % 畫出立體網狀圖

surf和mesh的用法類似:

x=linspace(-2, 2, 25); % 在x軸上取25點
y=linspace(-2, 2, 25); % 在y軸上取25點
[xx,yy]=meshgrid(x, y); % xx和yy都是21x21的矩陣
zz=xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz也是21x21的矩陣
surf(xx, yy, zz); % 畫出立體曲面圖

爲了方便測試立體繪圖,MATLAB提供了一個peaks函數,可產生一個凹凸有致的曲面,包含了三個局部極大點及三個局部極小點,其方程式爲:

z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)

要畫出此函數的最快方法即是直接鍵入peaks:

亦可對peaks函數取點,再以各種不同方法進行繪圖meshz可將曲面加上圍裙:

[x,y,z]=peaks;
meshz(x,y,z);
axis([-inf inf -inf inf -inf inf]);

waterfall可在x方向或y方向產生水流效果:

[x,y,z]=peaks;
waterfall(x,y,z);
axis([-inf inf -inf inf -inf inf]);

下列命令產生在y方向的水流效果: 

[x,y,z]=peaks;
waterfall(x',y',z');
axis([-inf inf -inf inf -inf inf]);

meshc同時畫出網狀圖與等高線:

[x,y,z]=peaks;
meshc(x,y,z);
axis([-inf inf -inf inf -inf inf]);

surfc同時畫出曲面圖與等高線:

[x,y,z]=peaks;
surfc(x,y,z);
axis([-inf inf -inf inf -inf inf]);

contour3畫出曲面在三度空間中的等高線:

contour3(peaks, 20);
axis([-inf inf -inf inf -inf inf]);

contour畫出曲面等高線在XY平面的投影:

contour(peaks, 20);

plot3可畫出三度空間中的曲線:

t=linspace(0,20*pi, 501);
plot3(t.*sin(t), t.*cos(t), t);

亦可同時畫出兩條三度空間中的曲線:

t=linspace(0, 10*pi, 501);
plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos
SURFL的z-參數。看z-參數的確定平滑塗色效果(定義變化方向)surfl(p,z);z=(n1,n2,n3)。           
clf;
x= -0.5:0.3:2.5;y=-0.5:0.3:2;
[X,Y]=meshgrid(x,y);
p=(4+X.^2/9+Y.^2/4);;
%cm=[1 0 0;0 1 0;0 0 1];
%colormap(cm)
subplot(2,2,1);surfl(p,[1,0,0])
subplot(2,2,2);surfl(p,[0,1,0])
subplot(2,2,3);surfl(p,[0,0,1])
subplot(2,2,4);surfl(p,[1,1,0])
(4) 輔助圖視效果
*1)視角定義view(az,el)
clf;x= -1.5:0.2:1.5;y=-1:0.2:1;
[X,Y]=meshgrid(x,y);
p=sqrt(4-X.^2/9-Y.^2/4);
subplot(2,2,1);surfl(p);view(30,30)
shading interp
subplot(2,2,2);surfl(p);view(90,10)
shading interp
subplot(2,2,3);surfl(p);view(-10,-10)
shading interp
subplot(2,2,4);surfl(p);view(140,60)
shading interp  
*2)surfl光照模式與光照角度設置, surfl(x,y,z,d,s,k)指令中s 與k 參數
d:見(3)
s:確定光照角度;z=(sx,sy,sz);默認光照角度是觀察角逆時針方向45度
k::光照模式:確定強度
       ka:背景光          kd:漫射光        ks:定向光        spread:擴散光
例:
clf;x= -1.5:0.2:1.5;y=-1:0.2:1;
[X,Y]=meshgrid(x,y);
Z=sqrt(4-X.^2/9-Y.^2/4);
view(45,45)
subplot(2,2,1);surfl(X,Y,Z, [0,45],[.1 .6 .4 10]);
shading interp
subplot(2,2,2);surfl(X,Y,Z, [20,45],[.3 .6 .4 10]);
shading interp
subplot(2,2,3);surfl(X,Y,Z, [40,45],[.6 .6 .4 10]);
shading interp
subplot(2,2,4);surfl(X,Y,Z, [60,45],[.9 .6 .4 10]);
shading interp
 
*3) 圖視放大 zoom on ;zoom off; zoom
*鼠標點擊變焦(左鍵放大;右鍵盤縮小)
*鼠標拖拉變焦
t=-16:0.1:16;
x=sin(t.*10).*(t.^2);
plot(t,x,'r-')
zoom on    
4. 超維圖形表達
(1) 三維色彩表達(色軸;圖象的色彩維)
clf
a=ones(20);
a1=2*ones(13);
a2=3*ones(7);
a3=4*ones(2);
a(4:16,4:16)=a1;
a(7:13,7:13)=a2;
a(10:11,10:11)=a3;
subplot(2,1,1)
meshc(a)
subplot(2,1,2)
pcolor(a)
colorbar('horiz')
colormap(hsv)
% shading interp

(2) 四維色彩表達(色軸;圖象的色彩維)
clf
x=-5:0.1:5;
y=-5:0.25:5;
z=-5:0.25:5;
n=length(x);
[X,Y,Z]=meshgrid(x,y,z);
V=(-X.^2-Y.^2-Z.^2);
xi=[-4,-2,0,2,4];
yi=0.5;
zi=-0.5;
subplot(2,1,1);
slice(x,y,z,V,xi,yi,zi);
colorbar('horiz');
view([45,45]);
shading interp
xi=[0];
subplot(2,1,2);
slice(x,y,z,V,xi,yi,zi);
view([30,45]);
shading interp    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章