【源码】PLOTCUBE——绘制三维立方体的函数
PLOTCUBE - 在当前坐标轴中显示三维立方体的绘图
PLOTCUBE - Display a 3D-cube in the current axes
PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR)
-
EDGES : 定义立方体边长的三元素向量
-
EDGES : 3-elements vector that defines the length of cube edges
-
ORIGIN: 定义立方体起点的三元素向量
-
ORIGIN: 3-elements vector that defines the start point of the cube
-
ALPHA : 定义立方体表面透明度的标量(范围从0到1)
-
ALPHA : scalar that defines the transparency of the cube faces (from 0 to 1)
-
COLOR : 定义立方体表面颜色的三元素向量
-
COLOR : 3-elements vector that defines the faces color of the cube
使用示例:
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]);
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);
源码下载地址:
http://page2.dfpan.com/fs/blcej2a21f29216dde4/
更多精彩文章请关注微信号: