溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Matlab如何實現三維投影繪制

發布時間:2022-08-12 10:54:46 來源:億速云 閱讀:737 作者:iii 欄目:開發技術

這篇文章主要講解了“Matlab如何實現三維投影繪制”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Matlab如何實現三維投影繪制”吧!

使用方法

三維多邊形投影及基本使用

通過介紹如何生成三維多邊形投影介紹一下函數咋用,這里的三維多邊形指的是使用patchfill3創建的圖形,假設我們繪制了如下一個復雜多邊形構成的球:

c = 1;
d = 2*c*(3*sqrt(2)+2)/7;
m = d*(3*sqrt(2)-4)/4;
l = m+d*(2-sqrt(2))/4; 
t = d*(2-sqrt(2))/4;
V = [c-l -t c; t -c+l c; -t -c+l c; -c+l -t c; -c+l t c; -t c-l c; t c-l c; c-l t c; c t c-l;...
    c -t c-l; c -c+l t; c -c+l -t; c -t -c+l; c t -c+l; c c-l -t; c c-l t; c-l c t; c-l c -t;...
    t c -c+l; -t c -c+l; -t c-l -c; t c-l -c; c-l t -c; -c+l t -c; -c+l c -t; -c c-l -t; -c t -c+l;...
    -c -t -c+l; -c+l -t -c; -t c c-l; t c c-l; -c+l c t; -c c-l t; -c t c-l; -c -t c-l; -c -c+l t;...
    -c+l -c t; -c+l -c -t; -c -c+l -t; -t -c -c+l; -t -c+l -c; t -c+l -c; t -c -c+l; c-l -c -t;...
    c-l -t -c; t -c c-l; -t -c c-l; c-l -c t];
F8 = [1 2 3 4 5 6 7 8; 17 18 19 20 25 32 30 31; 22 23 45 42 41 29 24 21;...
    37 38 40 43 44 48 46 47; 9 10 11 12 13 14 15 16; 26 27 28 39 36 35 34 33];
F6 = [5 6 30 32 33 34; 3 4 35 36 37 47; 1 2 46 48 11 10; 7 8 9 16 17 31; 20 25 26 27 24 21;...
    28 39 38 40 41 29; 14 23 22 19 18 15; 12 13 45 42 43 44];
F4 = [6 7 31 30; 1 10 9 8; 2 3 47 46; 4 5 34 35; 32 25 26 33; 36 37 38 39;...
    11 12 44 48; 15 16 17 18; 19 20 21 22; 27 24 29 28; 40 41 42 43; 13 14 23 45];
hold on;axis equal;grid on
axis([-1.5,1.5,-1.5,1.5,-1.5,1.5])
view(3);
patch('Vertices', V, 'Faces', F4, 'FaceColor', [0 .8 .9]);
patch('Vertices', V, 'Faces', F6, 'FaceColor', [0 .5 .9]);
patch('Vertices', V, 'Faces', F8, 'FaceColor', [0 .5 .5]);

Matlab如何實現三維投影繪制

在代碼最后加入這么一行即可生成投影:

axProjection3D('XYZ')

Matlab如何實現三維投影繪制

只生成部分投影:

axProjection3D('XZ')

Matlab如何實現三維投影繪制

為每個投影設置不同顏色:

axProjection3D('X') 
axProjection3D('Y',[.7,0,0]) 
axProjection3D('Z',[0,0,.7])

Matlab如何實現三維投影繪制

以上是工具函數的基本使用,以下給出應對其他幾種圖像格式該工具的使用效果:

三維曲面投影

此部分主要是值使用函數surf、surface、mesh函數創建的曲面,完全相同的使用方式:

cplxdemo
axis([-1.5,1.5,-1.5,1.5,-1.5,1.5])

axProjection3D('XYZ')

Matlab如何實現三維投影繪制

三維曲線投影

此部分主要是值使用函數line、plot3函數創建的曲線,完全相同的使用方式:

[~,L]=ode45(@(t,L)Lorenz(t,L),0:.01:100,[1;1;1;10;28;8/3]); 
plot3(L(:,1),L(:,2),L(:,3))
grid on

axProjection3D('XYZ') 

function dL=Lorenz(t,L)
% L=[x;y;z;a;r;b];
% dL=[dx/dt;dy/dt;dz/dt;0,0,0];
% dz/dt=-a*(x-y)
% dy/dt=x*(r-z)-y
% dz/dt=x*y-b*z
dL=zeros([6,1]);
dL(1)=-L(4)*(L(1)-L(2));
dL(2)=L(1)*(L(5)-L(3))-L(2);
dL(3)=L(1)*L(2)-L(6)*L(3);
dL(4:6)=0;
end

Matlab如何實現三維投影繪制

三維參數曲線投影

此部分主要是值使用函數fplot3函數創建的曲線,完全相同的使用方式:

xt = @(t) exp(-t/10).*sin(5*t);
yt = @(t) exp(-t/10).*cos(5*t);
zt = @(t) t;
fplot3(xt,yt,zt,[-10 10])

axProjection3D('XYZ')

Matlab如何實現三維投影繪制

三維參數曲面投影

此部分主要是值使用函數fsurf函數創建的曲面,完全相同的使用方式:

syms u v;
r = @(u) 4 - 2*cos(u);
x = piecewise(u <= pi, -4*cos(u)*(1+sin(u)) - r(u)*cos(u)*cos(v),...
    u > pi, -4*cos(u)*(1+sin(u)) + r(u)*cos(v));
y = r(u)*sin(v);
z = piecewise(u <= pi, -14*sin(u) - r(u)*sin(u)*cos(v),...
    u > pi, -14*sin(u));
fsurf(x,y,z, [0 2*pi 0 2*pi]);
axis([-8,12,-8,12,-22,18])

axProjection3D('XYZ')

Matlab如何實現三維投影繪制

混合類型三維繪圖投影

多種類型圖像畫在一起:

xt = @(t) exp(-t/10).*sin(5*t);
yt = @(t) exp(-t/10).*cos(5*t);
zt = @(t) t;
fplot3(xt,yt,zt,[-10 10],'LineWidth',2)
hold on

[X,Y,Z] = peaks(30);
surf(X,Y,Z)
axis([-5,5,-5,5,-8,8])
axProjection3D('XYZ')

Matlab如何實現三維投影繪制

工具函數完整代碼

function axProjection3D(varargin)
% @author : slandarer
% 公眾號  : slandarer隨筆
% 知乎    : hikari


% 獲取參數
if isa(varargin{1},'matlab.graphics.axis.Axes')
    ax=varargin{1};varargin(1)=[];
else
    ax=gca;
end
hold(ax,'on')
ax.XLim=ax.XLim;
ax.YLim=ax.YLim;
ax.ZLim=ax.ZLim;
state=upper(varargin{1});
if length(varargin)>1
    faceColor=varargin{2};
else
    faceColor=[.5,.5,.5];
end
[~,state,~]=intersect('XYZ',state);

% 記錄子圖形對象
ChildrenList(length(ax.Children))=ax.Children(end);
for n=1:length(ax.Children)
    ChildrenList(n)=ax.Children(n);
end
for n=length(ChildrenList):-1:1
    if strcmp(ChildrenList(n).Tag,'AP3D')
        ChildrenList(n)=[];
    end
end

% 繪制投影
minLim=[ax.XLim(2),ax.YLim(2),ax.ZLim(1)];
for i=1:length(state)
    ii=state(i);
    for n=1:length(ChildrenList)
        switch true
            % Patch對象投影 
            case isa(ChildrenList(n),'matlab.graphics.primitive.Patch')
            tobj=copyobj(ChildrenList(n),ax);
            tobj.Vertices(:,ii)=minLim(ii);
            tobj.FaceColor=faceColor;
            tobj.FaceAlpha=.5;
            tobj.EdgeColor=faceColor./5;
            tobj.EdgeAlpha=.9;
            tobj.Tag='AP3D';
            % Surface對象投影
            case isa(ChildrenList(n),'matlab.graphics.chart.primitive.Surface')||isa(ChildrenList(n),'matlab.graphics.primitive.Surface')
            tobj=copyobj(ChildrenList(n),ax);
            switch ii
                case 1,tobj.XData(:,:)=minLim(ii);
                case 2,tobj.YData(:,:)=minLim(ii);
                case 3,tobj.ZData(:,:)=minLim(ii);
            end
            tobj.FaceColor=faceColor;
            tobj.FaceAlpha=.5;
            tobj.EdgeColor=faceColor./5;
            tobj.EdgeAlpha=.9;
            tobj.Tag='AP3D';
            % Line對象投影
            case isa(ChildrenList(n),'matlab.graphics.chart.primitive.Line')||isa(ChildrenList(n),'matlab.graphics.primitive.Line')
            tobj=copyobj(ChildrenList(n),ax);
            switch ii
                case 1,tobj.XData(:,:)=minLim(ii);
                case 2,tobj.YData(:,:)=minLim(ii);
                case 3,tobj.ZData(:,:)=minLim(ii);
            end
            tobj.Color=[faceColor,.5];
            tobj.Tag='AP3D';
            % 三維參數化曲線
            case isa(ChildrenList(n),'matlab.graphics.function.ParameterizedFunctionLine')
            tobj=copyobj(ChildrenList(n),ax);
            switch ii
                case 1,tobj.XFunction=@(t)t.*0+minLim(ii);
                case 2,tobj.YFunction=@(t)t.*0+minLim(ii);
                case 3,tobj.ZFunction=@(t)t.*0+minLim(ii);
            end
            tobj.Color=[faceColor,.5];
            tobj.Tag='AP3D';
            % 三維參數化曲面
            case isa(ChildrenList(n),'matlab.graphics.function.ParameterizedFunctionSurface')
            tobj=copyobj(ChildrenList(n),ax);
            switch ii
                case 1,tobj.XFunction=minLim(ii);
                case 2,tobj.YFunction=minLim(ii);
                case 3,tobj.ZFunction=minLim(ii);
            end
            tobj.FaceColor=faceColor;
            tobj.FaceAlpha=.5;
            tobj.EdgeColor=faceColor./5;
            tobj.Tag='AP3D';
        end
    end
end
end

感謝各位的閱讀,以上就是“Matlab如何實現三維投影繪制”的內容了,經過本文的學習后,相信大家對Matlab如何實現三維投影繪制這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女