用Matlab绘制三维RGB立方体模型

用Matlab绘制三维RGB立方体模型,matlab,matlab-figure,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

我写这段代码是为了画一个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);

需要更新颜色贴图,以使您的绘图与中的绘图相似。您不能简单地使用内置函数直接生成正确的序列。此外,调用
hsv(8)
会生成您不想要的其他颜色(在命令窗口中打印出来查看),但不包括纯白色或黑色。您可以使用
hsv(6)
并附加
[0 0]
[1 1]
,但您需要确保顺序与代码的其余部分一致(
fm
vm

下面是您的代码的修订版本–矩阵对每个顶点的颜色模式进行编码:

% 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];

% RGB colors for each vertex
cm = [0 0 0;
      0 1 0;
      1 1 0;
      1 0 0;
      0 0 1;
      0 1 1;
      1 1 1;
      1 0 1];

% Plot the cube - gives each face a different color and creates the cube at a convenient viewing angle
figure('Color','w')
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cm,'FaceColor','interp');
view(120,30);

% Plot axes
axis equal;
axis off;
d1 = 1.25;
line([0 0 0;d1 0 0],[0 0 0;0 d1 0],[0 0 0;0 0 d1],'Color','k','LineWidth',2);

% Label axes
d2 = 0.1;
text([0 1 0],[1 -d2 -d2],[-d2 0 1],'255','FontSize',11,'HorizontalAlignment','center');
text([0 d1 0],[d1 d2 d2],[d2 0 d1],{'R','G','B'},'FontSize',16);
这将生成一个如下所示的图形


当我完成代码时,@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
% 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);
但实际上你想要应用的是红色,绿色,蓝色,白色,青色,品红色,黄色,黑色。请参考了解Matlab颜色代码。因此,我们可以根据您的要求手动将颜色应用于每个顶点。我改变了你的代码如下

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
% 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);
输出:


电流输出有什么问题?你想在每个脸上都涂上恒定(平坦)的颜色吗?或者是hsv的彩色地图有问题吗?没有平面给我每张脸一种颜色我需要它就像这张图片,我认为hsv有问题?!有什么问题吗?请问,你看到上传的图片了吗?它和画出来的有什么不同!我不知道是什么问题!!乞求帮助并不会让人们在工作上帮助你。改进和澄清您的问题(例如,将您的评论中的图像添加到您的问题中)将有助于解决问题。那么,不要期待即时的回应。这不是一个为你做事的服务。它依靠高质量的问答、沟通和专业精神而蓬勃发展。顶点的颜色与中的颜色不太匹配,尽管我不确定这有多重要。@horchler是的,通过与给定的图形完全匹配颜色,您已经做了大量工作。我建议您作为答案,因为它与问题很匹配。