MATLAB绘制 “甲基橙“分子示例图(整活)
作者:互联网
近期确实是比较忙,更得也比较慢,这次整个活:
甲基橙 hiahihia
效果:
注意,请将以下图片命名为surf.png并放在m文件所在文件夹:
自己截图拼的橙子皮,大家有更好看的可以自行替换:
原理也很简单,只是画一些桶装曲面和球状曲面拼在一起并打光,橙子的话贴个图,
完整代码:
function CH4_Orange
% @author:slandarer
% 仅供娱乐(甲基橙结构式!):
% -----------------+
% H |
% \ |
% H — C — Orange |
% / |
% H |
% -----------------+
% axes属性设置
ax=gca;
hold(ax,'on')
ax.PlotBoxAspectRatio=[1 1 1];
ax.PlotBoxAspectRatioMode='manual';
ax.XLim=[-4 4];
ax.YLim=[-4 4];
ax.ZLim=[-5 3];
ax.View=[45,45];
ax.XGrid='on';
ax.YGrid='on';
ax.ZGrid='on';
ax.Color=[0 0 0];
% =========================================================================
% 绕轴旋转矩阵
yaw_z=2*pi/3;
roll_x=acos(1/3);
R_z=[cos(yaw_z),-sin(yaw_z),0;
sin(yaw_z) ,cos(yaw_z) ,0;
0 ,0 ,1];
R_x=[1,0 , 0;
0,cos(roll_x),-sin(roll_x);
0,sin(roll_x),cos(roll_x)];
% -------------------------------------------------------------------------
% 绘制C原子
[Xball,Yball,Zball]=sphere(300); % 制作球形曲面
surf(Xball,Yball,Zball,'FaceColor',[.2,.2,.2],'EdgeColor','none')
% 绘制三个H原子
[nX,nY,nZ]=rotateXYZ(Xball.*.8,Yball.*.8,Zball.*.8+2.5,R_x);
for k=1:3
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z);
surface(nX,nY,nZ,'FaceColor',[.95,.95,.95],'EdgeColor','none')
end
% 橘子表面图倒入
surfImg=imread('surf.png');
surfImg=imresize(surfImg,size(Zball));
% -------------------------------------------------------------------------
% 制作一个圆筒状曲面
[Tcol,Zcol]=meshgrid(0:(2*pi/100):2*pi,(1:100)./100);
Xcol=cos(Tcol);
Ycol=sin(Tcol);
% 绘制连接杆
surf(0+Xcol.*.3,0+Ycol.*.3,-1.7+Zcol,'FaceColor',[.3,.3,.3],'EdgeColor','none')
[nX,nY,nZ]=rotateXYZ(Xcol.*.3,Ycol.*.3,Zcol.*.4+0.95,R_x);
for k=1:3
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z);
surface(nX,nY,nZ,'FaceColor',[.3,.3,.3],'EdgeColor','none')
end
[nX,nY,nZ]=rotateXYZ(Xcol.*.3,Ycol.*.3,Zcol.*.4+1.35,R_x);
for k=1:3
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z);
surface(nX,nY,nZ,'FaceColor',[.98,.98,.98],'EdgeColor','none')
end
% 打光
light;
material shiny %让上面所有曲面都有镜面质感
% 在最后画橙子,将镜面反射调低,漫反射调高,让他有布料质感
surf(Xball.*2,Yball.*2,-3+Zball.*1.7,'CData',surfImg,...
'EdgeColor','none','SpecularStrength',.0001,'DiffuseStrength',.7)
% =========================================================================
% 点绕轴旋转函数
function [nX,nY,nZ]=rotateXYZ(X,Y,Z,R)
nX=zeros(size(X));
nY=zeros(size(Y));
nZ=zeros(size(Z));
for i=1:size(X,1)
for j=1:size(X,2)
v=[X(i,j);Y(i,j);Z(i,j)];
nv=R*v;
nX(i,j)=nv(1);
nY(i,j)=nv(2);
nZ(i,j)=nv(3);
end
end
end
% -------------------------------------------------------------------------
end
标签:end,整活,示例,rotateXYZ,nX,nY,nZ,MATLAB,ax 来源: https://blog.csdn.net/slandarer/article/details/121310451