Matlab 利用參數方程繪制空心球體


  • 基本原理:
    實質為利用球面參數方程,利用網格化數據繪制
x=R*sin(theta)*cos(phi)
y=R*sin(theta)*sin(phi)
z=R*cos(theta)
  • 繪制函數:
function draw_sphere(rgb)
%此函數旨在繪制各種顏色的球面
%rgb為顏色參數,為三個0~1之間的三個數組成的數組
%such as:    [1,0,0],  [1,0.2,0.5], [0,1,0.5]
%you may run as :  draw_sphere([1,0,0])
%author:   楊文波 12/16/2016
t=linspace(0,2*pi,100*pi);
p=linspace(0,2*pi,100*pi);
[theta,phi]=meshgrid(t,p);                  %網格化數據
R=1;                                        %設置球面半徑
x=R*sin(theta).*cos(phi);                   %代入參數方程
y=R*sin(theta).*sin(phi);
z=R*cos(theta);
colormap(rgb);
surf(x,y,z);                                %繪制表面圖
daspect([1,1,1]);                           %設置xyz軸比例為1:1:1
camlight;                                   %設置默認光照
shading interp;
axis off;                                   %隱藏坐標軸
end

  • 簡單調用:
figure(1)
draw_sphere([1,0,0.5]);
figure(2)
draw_sphere([0,0,1]);
NOTE:
不推薦使用subplot分割繪圖,因為colormap作用域為整個figure
  • 結果展示:

1.rgb=[1,0,0.5]時:

hellow

2.rgb=[0,0,1]時:

這里寫圖片描述


免責聲明!

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



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