MATLAB画3x3立方体,代码如下:
for i = 1:9
if((mod(i,3)==0))
ii = 2;
else
ii = mod(i,3)-1;
end
plotcube([1 1 1],[ 2+ii 2+ceil(i/3) 2],.8,[1 0 0]);
end
for i = 1:9
if((mod(i,3)==0))
ii = 2;
else
ii = mod(i,3)-1;
end
plotcube([1 1 1],[ 2+ii 2+ceil(i/3) 3],.8,[0 1 0]);
end
for i = 1:9
if((mod(i,3)==0))
ii = 2;
else
ii = mod(i,3)-1;
end
plotcube([1 1 1],[ 2+ii 2+ceil(i/3) 4],.8,[0 0 1]);
end
function plotcube(varargin)
% PLOTCUBE - Display a 3D-cube in the current axes
%
% PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes
% with the following properties:
% * EDGES : 3-elements vector that defines the length of cube edges
% * ORIGIN: 3-elements vector that defines the start point of the cube
% * ALPHA : scalar that defines the transparency of the cube faces (from 0
% to 1)
% * COLOR : 3-elements vector that defines the faces color of the cube
%
% Example:
% >> plotcube([5 5 5],[ 2 2 2],.8,[1 0 0]);
% >> plotcube([5 5 5],[10 10 10],.8,[0 1 0]);
% >> plotcube([5 5 5],[20 20 20],.8,[0 0 1]);
% Default input arguments
inArgs = { ...
[10 56 100] , ... % Default edge sizes (x,y and z)
[10 10 10] , ... % Default coordinates of the origin point of the cube
.7 , ... % Default alpha value for the cube's faces
[1 0 0] ... % Default Color for the cube
};
% Replace default input arguments by input values
inArgs(1:nargin) = varargin;
% Create all variables
[edges,origin,alpha,clr] = deal(inArgs{:});
XYZ = { ...
[0 0 0 0] [0 0 1 1] [0 1 1 0] ; ...
[1 1 1 1] [0 0 1 1] [0 1 1 0] ; ...
[0 1 1 0] [0 0 0 0] [0 0 1 1] ; ...
[0 1 1 0] [1 1 1 1] [0 0 1 1] ; ...
[0 1 1 0] [0 0 1 1] [0 0 0 0] ; ...
[0 1 1 0] [0 0 1 1] [1 1 1 1] ...
};
XYZ = mat2cell(...
cellfun( @(x,y,z) x*y+z , ...
XYZ , ...
repmat(mat2cell(edges,1,[1 1 1]),6,1) , ...
repmat(mat2cell(origin,1,[1 1 1]),6,1) , ...
'UniformOutput',false), ...
6,[1 1 1]);
cellfun(@patch,XYZ{1},XYZ{2},XYZ{3},...
repmat({clr},6,1),...
repmat({'FaceAlpha'},6,1),...
repmat({alpha},6,1)...
);
view(3);
end
效果如下:
MATLAB画六种颜色六面体,代码如下:
a=[0;1;1;0];
b=[0;0;1;1];
c=[0;0;0;0];
d=[1;1;1;1];
x=[a a a a c d];
y=[b b c d a a];
z=[c d b b b b];
patch(x,y,z,1:6,'FaceAlpha',0.85);
view(3)
效果如下: