1 效果
2 程序说明
其实非常简单,只需要计算出各个顶点位置,并用fill3绘制一个个面出来即可,为了钻石的色彩更加丰富,我们用light函数设置了光照并为fill3创建出的PATCH对象设置了以下属性。
● FaceAlpha 面透明度
● EdgeAlpha 边透明度
● FaceLighting 面照亮方式(是整个面相同颜色还是通过插值不同位置不同颜色)
● EdgeLighting 边照亮方式(与面照亮方式类似)
● SpecularStrength 镜面反射的强度
● DiffuseStrength 散射光的强度
● AmbientStrength 环境光的强度
其中,
若将PATCH属性由:
faceColor=[0 71 177];
facePara={faceColor./255,'FaceAlpha',0.6,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.3,...
'FaceLighting','gouraud','EdgeLighting','gouraud',...
'SpecularStrength',0.9,'DiffuseStrength',0.7,'AmbientStrength',0.7}; % 面参数
改为:
faceColor=[237 64 153];
facePara={faceColor./255,'FaceAlpha',0.4,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.2,...
'FaceLighting','gouraud','EdgeLighting','gouraud',...
'SpecularStrength',0.9,'DiffuseStrength',0.7,'AmbientStrength',0.6}; % 面参数
则钻石变成这个样子:
另,
大家可以猜猜下面这个钻石是咋画的:
3 完整代码
function diamond
hold on
ax=gca;
axis equal
grid on
ax.GridLineStyle='--';
ax.LineWidth=1.2;
ax.XLim=[-2,2];
ax.YLim=[-2,2];
ax.ZLim=[-1.8,1.5];
ax.XColor=[1,1,1].*0.4;
ax.YColor=[1,1,1].*0.4;
ax.ZColor=[1,1,1].*0.4;
ax.DataAspectRatio=[1,1,1];
ax.DataAspectRatioMode='manual';
ax.CameraPosition=[-67.6287 -204.5276 82.7879];
light('Position',[5,6,5])
thetaSet8=linspace(0,2*pi-pi/4,8)';
thetaSet16=linspace(0,2*pi-pi/8,16)';
faceColor=[0 71 177];
facePara={faceColor./255,'FaceAlpha',0.6,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.3,...
'FaceLighting','gouraud','EdgeLighting','gouraud',...
'SpecularStrength',0.9,'DiffuseStrength',0.7,'AmbientStrength',0.7}; % 面参数
% faceColor=[237 64 153];
% facePara={faceColor./255,'FaceAlpha',0.4,'EdgeColor',faceColor./255.*0.8,'EdgeAlpha',0.2,...
% 'FaceLighting','gouraud','EdgeLighting','gouraud',...
% 'SpecularStrength',0.9,'DiffuseStrength',0.7,'AmbientStrength',0.6}; % 面参数
% 绘制最上方八边形
pntSet1=[cos(thetaSet8),sin(thetaSet8),ones(8,1)];
fill3(pntSet1(:,1),pntSet1(:,2),pntSet1(:,3),facePara{:})
% 一圈8个三角形
pntSet2=[cos(thetaSet8+pi/8).*1.3,sin(thetaSet8+pi/8).*1.3,ones(8,1).*0.7];
for i=1:8
p1=i;
p2=mod(i,8)+1;
p3=i;
fill3([pntSet1([p1,p2],1);pntSet2(p3,1)],...
[pntSet1([p1,p2],2);pntSet2(p3,2)],...
[pntSet1([p1,p2],3);pntSet2(p3,3)],facePara{:})
end
% 一圈8个四边形
pntSet3=[cos(thetaSet8).*1.7,sin(thetaSet8).*1.7,ones(8,1).*0.3];
for i=1:8
p1=mod(i,8)+1;
p2=i;
p3=p1;
p4=p1;
fill3([pntSet1(p1,1);pntSet2(p2,1);pntSet3(p3,1);pntSet2(p4,1)],...
[pntSet1(p1,2);pntSet2(p2,2);pntSet3(p3,2);pntSet2(p4,2)],...
[pntSet1(p1,3);pntSet2(p2,3);pntSet3(p3,3);pntSet2(p4,3)],facePara{:})
end
% 一圈16个三角形
pntSet4=[cos(thetaSet16).*1.7,sin(thetaSet16).*1.7,ones(16,1).*0.3];
for i=1:8
p1=i;
p2=2*i-1;
p3=2*i;
fill3([pntSet2(p1,1);pntSet4([p2,p3],1)],...
[pntSet2(p1,2);pntSet4([p2,p3],2)],...
[pntSet2(p1,3);pntSet4([p2,p3],3)],facePara{:})
p2=2*i;
p3=mod(2*i,16)+1;
fill3([pntSet2(p1,1);pntSet4([p2,p3],1)],...
[pntSet2(p1,2);pntSet4([p2,p3],2)],...
[pntSet2(p1,3);pntSet4([p2,p3],3)],facePara{:})
end
% 一圈16个四边角形
pntSet5=[cos(thetaSet16).*1.7,sin(thetaSet16).*1.7,ones(16,1).*0.15];
for i=1:16
p1=i;p2=mod(i,16)+1;
p3=mod(i,16)+1;p4=i;
fill3([pntSet4(p1,1);pntSet4(p2,1);pntSet5(p3,1);pntSet5(p4,1)],...
[pntSet4(p1,2);pntSet4(p2,2);pntSet5(p3,2);pntSet5(p4,2)],...
[pntSet4(p1,3);pntSet4(p2,3);pntSet5(p3,3);pntSet5(p4,3)],facePara{:})
end
pntSet6=[cos(thetaSet8+pi/8).*0.4,sin(thetaSet8+pi/8).*0.4,ones(8,1).*(-1.15)];
% 一圈16个长三角形
for i=1:8
p1=i;
p2=2*i-1;
p3=2*i;
fill3([pntSet6(p1,1);pntSet5([p2,p3],1)],...
[pntSet6(p1,2);pntSet5([p2,p3],2)],...
[pntSet6(p1,3);pntSet5([p2,p3],3)],facePara{:})
p2=2*i;
p3=mod(2*i,16)+1;
fill3([pntSet6(p1,1);pntSet5([p2,p3],1)],...
[pntSet6(p1,2);pntSet5([p2,p3],2)],...
[pntSet6(p1,3);pntSet5([p2,p3],3)],facePara{:})
end
% 一圈8个长四边形
pntSet7=[cos(thetaSet8).*1.7,sin(thetaSet8).*1.7,ones(8,1).*0.15];
for i=1:8
p1=mod(i,8)+1;
p2=i;
p4=mod(i,8)+1;
fill3([pntSet7(p1,1);pntSet6(p2,1);0;pntSet6(p4,1)],...
[pntSet7(p1,2);pntSet6(p2,2);0;pntSet6(p4,2)],...
[pntSet7(p1,3);pntSet6(p2,3);-1.5;pntSet6(p4,3)],facePara{:})
end
end
转自CSDN,作者「slandarer」
原文链接:https://blog.csdn.net/slandarer/article/details/119521516