如何在Matlab中绘制球体时定义半径?

4

我需要绘制多个球体,我正在使用来自MathWorks帮助的示例代码,如下所示 -

figure
[x,y,z] = sphere();
surf(x,y,z)  % sphere centered at origin
hold on
surf(x+3,y-2,z)  % sphere centered at (3,-2,0)
surf(x,y+1,z-3)  % sphere centered at (0,1,-3)
daspect([1 1 1])

我需要这些球拥有不同的半径。我该如何为每个球定义半径?

2个回答

9

[sphere]的帮助文件(http://www.mathworks.com.au/help/techdoc/ref/sphere.html)中提到,它生成单位球体(unit sphere)的坐标,即半径为1的球体。如果要将半径为1的球体坐标转换为半径为r的球体坐标,只需要将它们乘以r

[x,y,z] = sphere();
r = 5;
surf( r*x, r*y, r*z ) % sphere with radius 5 centred at (0,0,0)    

2
我认为,surf()不够用户友好。代码surf(x+3,y-2,z) % sphere centered at (3,-2,0)是不符合直觉的(surf(x-1,y+2,0)与数学一致)。
总之,我建议你使用ellipsoid()。由于球体只是椭球体的一个特例,你可以轻松理解它,而且不必处理surf(),请参见http://www.mathworks.com/help/matlab/ref/ellipsoid.html 一个简单的例子:
r=5;
[x,y,z]=ellipsoid(1,2,3,r,r,r,20);
surf(x, y, z,'FaceColor','y', 'FaceAlpha', 0.2);
axis equal;
box on; xlabel('x-axis (m)'); ylabel('y-axis (m)'); zlabel('z-axis (m)');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接