From: Meagan on
hi all,

I am not really sure the best way to pose my question so I have attached my code for all to look at. Basically what my code does is create a cylinder based on the vertices and faces that I have defined. Now, I am trying to figure out how to assign different colors to each of the faces. For example, I want faces 1-10 to be red and face 11-30 to be blue and 31-170 to be green etc. I can assign the entire cylinder to one solid color but I am not quite sure how to go about assigning each face a different color. Any help would be greatly appreciated.

Meagan




cylinder.vertices=[-1,0,-1;-1,0,0;-0.900000000000000,-0.433829802104045,-1;-0.900000000000000,-0.433829802104045,0;-0.916402259532854,-0.400000000000000,-1;-0.916402259532854,-0.400000000000000,0;-0.953820793572852,-0.300000000000000,-1;-0.953820793572852,-0.300000000000000,0;-0.979760845560207,-0.200000000000000,-1;-0.979760845560207,-0.200000000000000,0;-0.994984806594502,-0.100000000000000,-1;-0.994984806594502,-0.100000000000000,0;-0.994984806594502,0.100000000000000,-1;-0.994984806594502,0.100000000000000,0;-0.979760845560207,0.200000000000000,-1;-0.979760845560207,0.200000000000000,0;-0.953820793572852,0.300000000000000,-1;-0.953820793572852,0.300000000000000,0;-0.916402259532854,0.400000000000000,-1;-0.916402259532854,0.400000000000000,0;-0.900000000000000,0.433829802104045,-1;-0.900000000000000,0.433829802104045,0;-0.800000000000000,-0.600000000000000,-1;-0.800000000000000,-0.600
000000000000,0;-0.865690189633714,-0.500000000000000,-1;-0.865690189633714,-0.500000000000000,0;-0.865690189633714,0.500000000000000,-1;-0.865690189633714,0.500000000000000,0;-0.800000000000000,0.600000000000000,-1;-0.800000000000000,0.600000000000000,0;-0.700000000000000,-0.713755552299018,-1;-0.700000000000000,-0.713755552299018,0;-0.713755552299018,-0.700000000000000,-1;-0.713755552299018,-0.700000000000000,0;-0.713755552299018,0.700000000000000,-1;-0.713755552299018,0.700000000000000,0;-0.700000000000000,0.713755552299018,-1;-0.700000000000000,0.713755552299018,0;-0.600000000000000,-0.800000000000000,-1;-0.600000000000000,-0.800000000000000,0;-0.600000000000000,0.800000000000000,-1;-0.600000000000000,0.800000000000000,0;-0.500000000000000,-0.865690189633714,-1;-0.500000000000000,-0.865690189633714,0;-0.500000000000000,0.865690189633714,-1;-0.500000000000000,0.865690189633714,0;-0.400
000000000000,-0.916402259532854,-1;-0.400000000000000,-0.916402259532854,0;-0.433829802104045,-0.900000000000000,-1;-0.433829802104045,-0.900000000000000,0;-0.433829802104045,0.900000000000000,-1;-0.433829802104045,0.900000000000000,0;-0.400000000000000,0.916402259532854,-1;-0.400000000000000,0.916402259532854,0;-0.300000000000000,-0.953820793572852,-1;-0.300000000000000,-0.953820793572852,0;-0.300000000000000,0.953820793572852,-1;-0.300000000000000,0.953820793572852,0;-0.200000000000000,-0.979760845560207,-1;-0.200000000000000,-0.979760845560207,0;-0.200000000000000,0.979760845560207,-1;-0.200000000000000,0.979760845560207,0;-0.100000000000000,-0.994984806594502,-1;-0.100000000000000,-0.994984806594502,0;-0.100000000000000,0.994984806594502,-1;-0.100000000000000,0.994984806594502,0;0,-1,-1;0,-1,0;0,1,-1;0,1,0;0.100000000000000,-0.994984806594502,-1;0.100000000000000,-0.994984806594502,0
;0.100000000000000,0.994984806594502,-1;0.100000000000000,0.994984806594502,0;0.200000000000000,-0.979760845560207,-1;0.200000000000000,-0.979760845560207,0;0.200000000000000,0.979760845560207,-1;0.200000000000000,0.979760845560207,0;0.300000000000000,-0.953820793572852,-1;0.300000000000000,-0.953820793572852,0;0.300000000000000,0.953820793572852,-1;0.300000000000000,0.953820793572852,0;0.400000000000000,-0.916402259532854,-1;0.400000000000000,-0.916402259532854,0;0.400000000000000,0.916402259532854,-1;0.400000000000000,0.916402259532854,0;0.433829802104045,-0.900000000000000,-1;0.433829802104045,-0.900000000000000,0;0.500000000000000,-0.865690189633714,-1;0.500000000000000,-0.865690189633714,0;0.500000000000000,0.865690189633714,-1;0.433829802104045,0.900000000000000,-1;0.500000000000000,0.865690189633714,0;0.433829802104045,0.900000000000000,0;0.600000000000000,-0.800000000000000,-1;0.
600000000000000,-0.800000000000000,0;0.600000000000000,0.800000000000000,-1;0.600000000000000,0.800000000000000,0;0.700000000000000,-0.713755552299018,-1;0.700000000000000,-0.713755552299018,0;0.700000000000000,0.713755552299018,-1;0.700000000000000,0.713755552299018,0;0.713755552299018,-0.700000000000000,-1;0.713755552299018,-0.700000000000000,0;0.800000000000000,-0.600000000000000,-1;0.800000000000000,-0.600000000000000,0;0.800000000000000,0.600000000000000,-1;0.713755552299018,0.700000000000000,-1;0.800000000000000,0.600000000000000,0;0.713755552299018,0.700000000000000,0;0.865690189633714,-0.500000000000000,-1;0.865690189633714,-0.500000000000000,0;0.900000000000000,-0.433829802104045,-1;0.900000000000000,-0.433829802104045,0;0.900000000000000,0.433829802104045,-1;0.865690189633714,0.500000000000000,-1;0.900000000000000,0.433829802104045,0;0.865690189633714,0.500000000000000,0;0.9164
02259532854,-0.400000000000000,-1;0.916402259532854,-0.400000000000000,0;0.953820793572852,-0.300000000000000,-1;0.953820793572852,-0.300000000000000,0;0.979760845560207,-0.200000000000000,-1;0.979760845560207,-0.200000000000000,0;0.994984806594502,-0.100000000000000,-1;0.994984806594502,-0.100000000000000,0;1,0,-1;1,0,0;0.994984806594502,0.100000000000000,-1;0.994984806594502,0.100000000000000,0;0.979760845560207,0.200000000000000,-1;0.979760845560207,0.200000000000000,0;0.953820793572852,0.300000000000000,-1;0.953820793572852,0.300000000000000,0;0.916402259532854,0.400000000000000,-1;0.916402259532854,0.400000000000000,0;-1,0,1;-0.900000000000000,-0.433829802104045,1;-0.916402259532854,-0.400000000000000,1;-0.953820793572852,-0.300000000000000,1;-0.979760845560207,-0.200000000000000,1;-0.994984806594502,-0.100000000000000,1;-0.994984806594502,0.100000000000000,1;-0.979760845560207,0.20
0000000000000,1;-0.953820793572852,0.300000000000000,1;-0.916402259532854,0.400000000000000,1;-0.900000000000000,0.433829802104045,1;-0.800000000000000,-0.600000000000000,1;-0.865690189633714,-0.500000000000000,1;-0.865690189633714,0.500000000000000,1;-0.800000000000000,0.600000000000000,1;-0.700000000000000,-0.713755552299018,1;-0.713755552299018,-0.700000000000000,1;-0.713755552299018,0.700000000000000,1;-0.700000000000000,0.713755552299018,1;-0.600000000000000,-0.800000000000000,1;-0.600000000000000,0.800000000000000,1;-0.500000000000000,-0.865690189633714,1;-0.500000000000000,0.865690189633714,1;-0.400000000000000,-0.916402259532854,1;-0.433829802104045,-0.900000000000000,1;-0.433829802104045,0.900000000000000,1;-0.400000000000000,0.916402259532854,1;-0.300000000000000,-0.953820793572852,1;-0.300000000000000,0.953820793572852,1;-0.200000000000000,-0.979760845560207,1;-0.2000000000000
00,0.979760845560207,1;-0.100000000000000,-0.994984806594502,1;-0.100000000000000,0.994984806594502,1;0,-1,1;0,1,1;0.100000000000000,-0.994984806594502,1;0.100000000000000,0.994984806594502,1;0.200000000000000,-0.979760845560207,1;0.200000000000000,0.979760845560207,1;0.300000000000000,-0.953820793572852,1;0.300000000000000,0.953820793572852,1;0.400000000000000,-0.916402259532854,1;0.400000000000000,0.916402259532854,1;0.433829802104045,-0.900000000000000,1;0.500000000000000,-0.865690189633714,1;0.500000000000000,0.865690189633714,1;0.433829802104045,0.900000000000000,1;0.600000000000000,-0.800000000000000,1;0.600000000000000,0.800000000000000,1;0.700000000000000,-0.713755552299018,1;0.700000000000000,0.713755552299018,1;0.713755552299018,-0.700000000000000,1;0.800000000000000,-0.600000000000000,1;0.800000000000000,0.600000000000000,1;0.713755552299018,0.700000000000000,1;0.8656901896337
14,-0.500000000000000,1;0.900000000000000,-0.433829802104045,1;0.900000000000000,0.433829802104045,1;0.865690189633714,0.500000000000000,1;0.916402259532854,-0.400000000000000,1;0.953820793572852,-0.300000000000000,1;0.979760845560207,-0.200000000000000,1;0.994984806594502,-0.100000000000000,1;1,0,1;0.994984806594502,0.100000000000000,1;0.979760845560207,0.200000000000000,1;0.953820793572852,0.300000000000000,1;0.916402259532854,0.400000000000000,1;];
cylinder.faces=[3,4,5;5,4,6;5,6,7;7,6,8;7,8,9;9,8,10;9,10,11;11,10,12;11,12,1;1,12,2;1,2,13;13,2,14;13,14,15;15,14,16;15,16,17;17,16,18;17,18,19;19,18,20;19,20,21;21,20,22;23,24,25;25,24,26;25,26,3;3,26,4;21,22,27;27,22,28;27,28,29;29,28,30;31,32,33;33,32,34;33,34,23;23,34,24;29,30,35;35,30,36;35,36,37;37,36,38;39,40,31;31,40,32;37,38,41;41,38,42;43,44,39;39,44,40;45,41,46;46,41,42;47,48,49;49,48,50;49,50,43;43,50,44;45,46,51;51,46,52;51,52,53;53,52,54;55,56,47;47,56,48;57,53,58;58,53,54;59,60,55;55,60,56;61,57,62;62,57,58;63,64,59;59,64,60;65,61,66;66,61,62;67,68,63;63,68,64;65,66,69;69,66,70;67,71,68;68,71,72;73,69,74;74,69,70;75,76,71;71,76,72;77,73,78;78,73,74;79,80,75;75,80,76;81,77,82;82,77,78;83,84,79;79,84,80;85,81,86;86,81,82;83,87,84;84,87,88;87,89,88;88,89,90;91,92,93;93,92,94;92,85,94;94,85,86;95,96,89;89,96,90;97,91,98;98,91,93;95,99,96;96,99,100;101,97,102;102,97,98;99,103,
100;100,103,104;103,105,104;104,105,106;107,108,109;109,108,110;108,101,110;110,101,102;105,111,106;106,111,112;111,113,112;112,113,114;115,116,117;117,116,118;116,107,118;118,107,109;113,119,114;114,119,120;119,121,120;120,121,122;121,123,122;122,123,124;123,125,124;124,125,126;125,127,126;126,127,128;127,129,128;128,129,130;129,131,130;130,131,132;131,133,132;132,133,134;133,135,134;134,135,136;135,115,136;136,115,117;4,138,6;6,138,139;6,139,8;8,139,140;8,140,10;10,140,141;10,141,12;12,141,142;12,142,2;2,142,137;2,137,14;14,137,143;14,143,16;16,143,144;16,144,18;18,144,145;18,145,20;20,145,146;20,146,22;22,146,147;24,148,26;26,148,149;26,149,4;4,149,138;22,147,28;28,147,150;28,150,30;30,150,151;32,152,34;34,152,153;34,153,24;24,153,148;30,151,36;36,151,154;36,154,38;38,154,155;40,156,32;32,156,152;38,155,42;42,155,157;44,158,40;40,158,156;46,42,159;159,42,157;48,160,50;50,160,161;50,16
1,44;44,161,158;46,159,52;52,159,162;52,162,54;54,162,163;56,164,48;48,164,160;58,54,165;165,54,163;60,166,56;56,166,164;62,58,167;167,58,165;64,168,60;60,168,166;66,62,169;169,62,167;68,170,64;64,170,168;66,169,70;70,169,171;68,72,170;170,72,172;74,70,173;173,70,171;76,174,72;72,174,172;78,74,175;175,74,173;80,176,76;76,176,174;82,78,177;177,78,175;84,178,80;80,178,176;86,82,179;179,82,177;84,88,178;178,88,180;88,90,180;180,90,181;93,94,182;182,94,183;94,86,183;183,86,179;96,184,90;90,184,181;98,93,185;185,93,182;96,100,184;184,100,186;102,98,187;187,98,185;100,104,186;186,104,188;104,106,188;188,106,189;109,110,190;190,110,191;110,102,191;191,102,187;106,112,189;189,112,192;112,114,192;192,114,193;117,118,194;194,118,195;118,109,195;195,109,190;114,120,193;193,120,196;120,122,196;196,122,197;122,124,197;197,124,198;124,126,198;198,126,199;126,128,199;199,128,200;128,130,200;200,130,201
;130,132,201;201,132,202;132,134,202;202,134,203;134,136,203;203,136,204;136,117,204;204,117,194;];

count=renderpatch(cylinder);

set(gca,'projection','perspective')
set(gca,'CameraViewAngle',7)

axis on;
grid on
daspect([1 1 1])
light('position',[10,-10,10])
rotate3d on






function count = renderpatch(objIn)

if (iscell(objIn)) %a list of structs

for i=1:length(objIn)

obj=patch(objIn{i});

if (isfield(objIn{i},'facecolor'))
fcolor=objIn{i}.facecolor;
else
fcolor=[0,1,1];
end

if (isfield(objIn{i},'edgecolor'))
ecolor=objIn{i}.edgecolor;
else
ecolor='none';
end

if (isfield(objIn{i},'ambientstrength'))
ambstr=objIn{i}.ambientstrength;
else
ambstr=.6;
end

if (isfield(objIn{i},'specularstrength'))
spcstr=objIn{i}.specularstrength;
else
spcstr=.2;
end

if (isfield(objIn{i},'specularexponent'))
spcexp=objIn{i}.specularexponent;
else
spcexp=10;
end

if (isfield(objIn{i},'facelighting'))
facelight=objIn{i}.facelighting;
else
facelight='phong';
end

if (isfield(objIn{i},'diffusestrength'))
difstr=objIn{i}.diffusestrength;
else
difstr=.5;
end

if (isfield(objIn{i},'visible'))
vis=objIn{i}.visible;
else
vis='on';
end


set(obj, 'FaceColor', fcolor, ...
'EdgeColor', ecolor, ...
'AmbientStrength',ambstr,...
'SpecularStrength',spcstr, ...
'SpecularExponent', spcexp, ...
'FaceLighting', facelight, ...
'DiffuseStrength', difstr, ...
'Visible',vis);
end
count=i;

elseif (isstruct(objIn)) %must be a single struct
obj=patch(objIn);

if (isfield(objIn,'facecolor'))
fcolor=objIn.facecolor;
else
fcolor=[0,1,1];
end

if (isfield(objIn,'edgecolor'))
ecolor=objIn.edgecolor;
else
ecolor='none';
end

if (isfield(objIn,'ambientstrength'))
ambstr=objIn.ambientstrength;
else
ambstr=.6;
end

if (isfield(objIn,'specularstrength'))
spcstr=objIn.specularstrength;
else
spcstr=.2;
end

if (isfield(objIn,'specularexponent'))
spcexp=objIn.specularexponent;
else
spcexp=10;
end

if (isfield(objIn,'facelighting'))
facelight=objIn.facelighting;
else
facelight='phong';
end

if (isfield(objIn,'diffusestrength'))
difstr=objIn.diffusestrength;
else
difstr=.5;
end

if (isfield(objIn,'visible'))
vis=objIn.visible;
else
vis='on';
end

set(obj, 'FaceColor', fcolor, ...
'EdgeColor', ecolor, ...
'AmbientStrength',ambstr,...
'SpecularStrength',spcstr, ...
'SpecularExponent', spcexp, ...
'FaceLighting', facelight, ...
'DiffuseStrength', difstr, ...
'Visible',vis);
count=1;
end %if
 | 
Pages: 1
Prev: Linear indexing
Next: fopen error