在Matlab中填充顶部和底部的圆柱体 [英] Cylinder with filled top and bottom in matlab

查看:275
本文介绍了在Matlab中填充顶部和底部的圆柱体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个顶部和底部都已填充的实心"圆柱体.我知道有一个功能圆筒(r)创建了一个,尽管它没有顶部和底部的圆来关闭"它.

I am trying to create a "solid" cylinder that has a filled top and bottom to it. I know that there is the function cylinder(r) that creates one, though it does not have a top and bottom circle to "close it".

我做了一些研究,但似乎找不到能做到这一点的功能.我发现了这一点: http://www.mathworks.com/help/symbolic/mupad_ref/plot-cylinder.html 虽然是mupad代码,但我不知道如何从matlab(从我的.m文件)中调用该函数.再次,我做了一些研究,这是我发现的,尽管似乎没有用:

I did some research and can't seem to find a function that does this. I have found this: http://www.mathworks.com/help/symbolic/mupad_ref/plot-cylinder.html though it is mupad code, and I don't know how to call that function from matlab (from my .m file). Once again, I have done some research and this is what I have found, though is does not seem to work: http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html . Is this possible, and if so how? If not, how can I make my "solid" cylinder in matlab?

谢谢

推荐答案

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

Assuming a cylinder aligned with the z-axis, radii R linearly spaced along the unit height above the XY-plane (same assumptions as built-in 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]使用at创建的某些复合对象(带有圆柱体的圆锥体)至少两个不同的值):

To check whether points are inside a cylinder of height L (note: assuming a true 'cylinder' as created with [R R], and NOT some compound object (cones with cylinders) as created by [R1 R2 ... RN] with at least two different values):

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

这篇关于在Matlab中填充顶部和底部的圆柱体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆