matlab球体填充圆柱体,在matlab中填充顶部和底部的圆柱体

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

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的圆柱体内(注意:假设用[RR]创建的真正’圆柱’,而不是由[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值