我在MATLAB中生成了随机3D点.每次运行时点数组的长度都会发生变化.我想把这些点变成球体.但是,我还没有成功.我的观点的散点图是这样的:
每个点用x,y和z表示.现在,我想用x,y和z作为中心点并生成半径为r的球体?我怎样才能做到这一点?
为了给你一个想法,一个示例图片来展示我期望产生的东西:
你可以使用内置sphere,乘以半径并添加中心坐标.你可以使用cellfun以下方式绘制所有内容:
% number of spheres
n = 10;
% random xyz center points
xyz = rand(n,3)*10;
% random radius
r = rand(n,1);
% generate unit sphere (radius=1, center=[0,0,0])
[X,Y,Z] = sphere;
% plot function for all spheres
plotfun = @(c,r) surf(X*r + c(1),Y*r + c(2),Z*r + c(3));
% generate figure with "hold on"
figure;
hold on;
axis equal;
grid on;
% plot all spheres
h = cellfun(plotfun,num2cell(xyz,2),num2cell(r),'UniformOutput',0);
Run Code Online (Sandbox Code Playgroud)
如果您希望球体类似于您想要的输出,您可以添加一些图形属性surf并添加一个light对象:
plotfun = @(c,r) surf(x*r + c(1),y*r + c(2),z*r + c(3),...
'FaceColor',.7*[1 1 1],'EdgeColor','none',...
'FaceLighting','gouraud','AmbientStrength',0.5);
light('Position',[-1 0 0]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
773 次 |
| 最近记录: |