matlab画转体_【求助】matlab生成旋转体?

CODE:

clear

x = 0 : pi / 10 : 2 * pi;   % x范围

y = 2 + cos( x );   % y = f( x );

[ X, Y, Z ] = cylinder( y, 20 );    % 回转中心是z轴

subplot( 1, 3, 1 )  % 绕z轴旋转

surf( X, Y, Z )

xlabel( 'x' )

ylabel( 'y' )

zlabel( 'z' )

axis square

Sx = reshape( X, size( X, 1 ) * size( X, 2 ), 1 );

Sy = reshape( Y, size( Y, 1 ) * size( Y, 2 ), 1 );

Sz = reshape( Z, size( Z, 1 ) * size( Z, 2 ), 1 );

subplot( 1, 3, 2 ) % 将回转中心由z轴转到x轴,需绕y轴旋转90度

theta = pi / 2;

% 图形变换矩阵

M = [ cos( theta ), 0, -sin( theta );

0, 1, 0;

sin( theta ), 0, cos( theta ) ];

S1 = [ Sx, Sy, Sz ] * M;

X1 = reshape( S1( :, 1 ), size( X, 1 ), size( X, 2 ) );

Y1 = reshape( S1( :, 2 ), size( X, 1 ), size( X, 2 ) );

Z1 = reshape( S1( :, 3 ), size( X, 1 ), size( X, 2 ) );

surf( X1, Y1, Z1 )

xlabel( 'x' )

ylabel( 'y' )

zlabel( 'z' )

axis square

subplot( 1, 3, 3 )  % 将回转中心由z轴转到y轴,需绕x轴旋转-90度

theta = -pi / 2;

% 图形变换矩阵

M = [ 1, 0, 0;

0, cos( theta ), sin( theta );

0, -sin( theta ), cos( theta ) ];

S2 = [ Sx, Sy, Sz ] * M;

X2 = reshape( S2( :, 1 ), size( X, 1 ), size( X, 2 ) );

Y2 = reshape( S2( :, 2 ), size( X, 1 ), size( X, 2 ) );

Z2 = reshape( S2( :, 3 ), size( X, 1 ), size( X, 2 ) );

surf( X2, Y2, Z2 )

xlabel( 'x' )

ylabel( 'y' )

zlabel( 'z' )

axis square

你可能感兴趣的:(matlab画转体)