• 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]);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

    axProjection3D('XYZ') 
    
    • 1

    只生成部分投影:

    axProjection3D('XZ') 
    
    • 1

    为每个投影设置不同颜色:

    axProjection3D('X') 
    axProjection3D('Y',[.7,0,0]) 
    axProjection3D('Z',[0,0,.7]) 
    
    • 1
    • 2
    • 3

    以上是工具函数的基本使用,以下给出应对其他几种图像格式该工具的使用效果:


    三维曲面投影

    此部分主要是值使用函数surfsurfacemesh函数创建的曲面,完全相同的使用方式:

    cplxdemo
    axis([-1.5,1.5,-1.5,1.5,-1.5,1.5])
    
    axProjection3D('XYZ') 
    
    • 1
    • 2
    • 3
    • 4


    三维曲线投影

    此部分主要是值使用函数lineplot3函数创建的曲线,完全相同的使用方式:

    [~,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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18


    三维参数曲线投影

    此部分主要是值使用函数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')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6


    三维参数曲面投影

    此部分主要是值使用函数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') 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11


    混合类型三维绘图投影

    多种类型图像画在一起:

    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')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10


    工具函数完整代码

    function axProjection3D(varargin)
    
    
    % 获取参数
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    【Spring连载】使用Spring Data访问 MongoDB----对象映射之对象引用
    模拟电路定理
    《程序是怎样跑起来的》读书笔记1——对程序员来说CPU是什么
    【Node.js】 第四章 模块化
    Set与二分法效率
    低代码:时代的选择
    windows 安装 Oracle Database 19c
    fyne - 谁说用Go不能开发应用界面
    【深度学习】笔记2-神经网络
    Git常见命令
  • 原文地址:https://blog.csdn.net/slandarer/article/details/126284617