【问题标题】:Cylinder with filled top and bottom in matlabmatlab中填充顶部和底部的圆柱体
【发布时间】:2013-11-22 20:33:49
【问题描述】:

我正在尝试创建一个顶部和底部填充的“实心”圆柱体。我知道有一个函数 cylinder(r) 可以创建一个,尽管它没有顶部和底部的圆圈来“关闭它”。

我做了一些研究,但似乎找不到可以做到这一点的函数。我发现了这个:http://www.mathworks.com/help/symbolic/mupad_ref/plot-cylinder.html 虽然它是 mupad 代码,但我不知道如何从 matlab(从我的 .m 文件)调用该函数。再一次,我做了一些研究,这就是我发现的,虽然它似乎不起作用:http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html。这可能吗,如果可以,怎么办?如果没有,我怎样才能在 matlab 中制作我的“实心”圆柱体?

谢谢

【问题讨论】:

  • “制作实心圆柱体”对您意味着什么?您是否尝试绘制具有顶部和底部的 3D 对象?能不能不用patch画出你想要的顶面和底面(定义两个圆,画成多边形,然后填充)?

标签: matlab matlab-figure mupad


【解决方案1】:

假设一个圆柱体与z 轴对齐,半径R 沿XY 平面上方的单位高度线性间隔(与内置cylinder 的假设相同):

function [x,y,z] = solidCylinder(varargin)

    %// Basic checks
    assert(nargin >= 1, 'Not enough input arguments.');
    assert(nargin <= 3, 'Too many input arguments.');
    assert(nargout <= 3, 'Too many output arguments.');

    %// Parse input
    N  = 20;
    Ax = [];
    switch nargin
        case 1 %// R
            R  = varargin{1};
        case 2  %// Ax, R  or  R, N
            if ishandle(varargin{1})
                Ax = varargin{1};
                R  = varargin{2};                
            else
                R  = varargin{1};
                N  = varargin{2};
            end

        case 3 %// Ax, R, N
            Ax = varargin{1};
            R  = varargin{2};
            N  = varargin{3};
    end

    %// Check input arguments
    if ~isempty(Ax)
        assert(ishandle(Ax) && strcmp(get(Ax, 'type'), 'axes'),...
            'Argument ''Ax'' must be a valid axis handle.');        
    else
        Ax = gca;
    end

    assert(isnumeric(R) && isvector(R) && all(isfinite(R)) && all(imag(R)==0) && all(R>0),...
        'Argument ''R'' must be a vector containing finite, positive, real values.');    
    assert(isnumeric(N) && isscalar(N) && isfinite(N) && imag(N)==0 && N>0 && round(N)==N,...
        'Argument ''N'' must be a finite, postive, real, scalar integer.');

    %// Compute cylinder coords (mostly borrowed from builtin 'cylinder')   
    theta         = 2*pi*(0:N)/N;
    sintheta      = sin(theta); 
    sintheta(N+1) = 0;

    M = length(R);
    if M==1 
        R = [R;R]; M = 2; end

    x = R(:) * cos(theta);
    y = R(:) * sintheta;
    z = (0:M-1).'/(M-1) * ones(1,N+1);  %'

    if nargout == 0                
        oldNextPlot = get(Ax, 'NextPlot');         
        set(Ax, 'NextPlot', 'add');

        %// The side of the cylinder
        surf(x,y,z, 'parent',Ax); 
        %// The bottom 
        patch(x(1,:)  , y(1,:)  , z(1,:)  , z(1,:)  );
        %// The top
        patch(x(end,:), y(end,:), z(end,:), z(end,:));

        set(Ax, 'NextPlot', oldNextPlot);
    end

end

检查点是否在高度为L 的圆柱体内(注意:假设使用[R R] 创建的真正的“圆柱体”,而不是[R1 R2 ... RN] 创建的某些复合对象(带圆柱体的圆锥体))至少两个不同的值):

function p = pointInCylinder(x,y,z)

    %// These can also be passed by argument of course
    R = 10;
    L = 5;

    %// Basic checks
    assert(isequal(size(x),size(y),size(z)), ... 
        'Dimensions of the input arguments must be equal.');

    %// Points inside the circular shell? 
    Rs = sqrt(x.^2 + y.^2 + z.^2) <= R;
    %// Points inside the top and bottom? 
    Os = z>=0 & z<=L;

    p = Rs & Os;

end

【讨论】:

    【解决方案2】:

    以下是如何制作盖子:

    clear all
    close all
    
    r = 1;
    
    h = 2;
    
    theta = 0:0.05:2*pi;
    
    x = r*cos(theta);
    y = r*sin(theta);
    
    y(end) = 0;
    
    z1 = 0;
    
    z2 = h;
    
    patch(x,y,z1*ones(size(x)),'b');
    
    set(gca,'NextPlot','Add');
    
    patch(x,y,z2*ones(size(x)),'b');
    
    surf([x;x],[y;y],[z1*ones(size(x));z2*ones(size(x))],'parent',gca)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多