use*_*405 5 matlab matlab-figure
我写了这段代码来绘制RGB立方体,但是它的颜色不完全正确吗?
%Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8]
%Define an eight row by three column matrix to define the vertices at which
%the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]
%Plot the cube ----- gives each face a different color and creates the cube at a convenient viewing angle
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',hsv(8),'FaceColor','interp');
view(3);
Run Code Online (Sandbox Code Playgroud)

当我完成代码时,@horchler 的答案已经在线了。看起来很完美。无论如何也发布我的。
为了了解您所应用的颜色,我打印了 hsv(8) 的值,如下所示。
1.0000 0 0
1.0000 0.7500 0
0.5000 1.0000 0
0 1.0000 0.2500
0 1.0000 1.0000
0 0.2500 1.0000
0.5000 0 1.0000
1.0000 0 0.7500
Run Code Online (Sandbox Code Playgroud)
但实际上你要应用的是红、绿、蓝、白、青、品红、黄、黑。请参考此链接了解Matlab颜色代码。因此,我们可以根据您的要求手动将颜色应用于每个顶点。我更改了你的代码如下。
% Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8];
% Define an eight row by three column matrix to define the vertices at which
% the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];
% Plot the cube ----- gives each face a different color and creates the
% cube at a convenient viewing angle
clear cdata;
cdata = [
0 0 0; % black
1 0 0; % red
1 0 1; % magenta
0 0 1; % blue
0 1 0; % green
1 1 0; % yellow
1 1 1; % white
0 1 1; % cyan
];
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cdata,'FaceColor','interp');
axis equal;
axis off;
view(3);
Run Code Online (Sandbox Code Playgroud)
输出:
