原文作者:善良995
原文标题:Matlab玩出新高度,变身表白女友神器
发布时间:2021-03-19 13:36:02
Matlab还可以这样玩儿?每逢节日愁哭程序员,不知道该送什么给女朋友,在这里教你用Matlab玩儿出属于程序员的浪漫,送给她一整天的惊喜^^
一、效果图
先来看看效果图:
怎么样,这礼物是不是很用心?是不是很特别?是不是很程序猿?(斜眼笑~)
二、完整模板代码
当然,我怎么忍心让好男孩们千篇一律地复制–>粘贴–>发送……使用同一个动图呢,为满足广大男同胞们个性化定制,完整代码放在下面:
clear
clc
tic
%构造体积方程和坐标轴
[X,Y,Z] = meshgrid(linspace(-3,3,101));
%3D心型图方程
F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
%展开画布
hFigure=figure;
sz= get(hFigure, 'Position');
set(hFigure, 'Position', [sz(1)-0.15*sz(3) sz(2) 1.3*sz(3) sz(4)]);%设置画布大小
set(hFigure,'color','w', 'menu','none')%隐藏菜单,颜色做白
hAxes = axes('Parent',hFigure,...?
'DataAspectRatio',[1 1 1],...? ??
'XLim',[30 120], 'YLim',[35 65], 'ZLim',[30 75]);
view([-39 30]); %视角
axis off %关闭坐标轴
%hidden on
%制作出动态的隐形效果
p = patch(isosurface(F,0));
set(p,'FaceColor','none','EdgeColor','none'); %心形线和面隐藏
alpha(0); %使其为0透明度以便显示文字
text(45,50,60,'宝贝','fontweight','bold','fontsize',25,'color','r');%显示出文字
pause(2)
hold on
%描出Y-Z平面的心形轮廓
for iX = 35:1:67
plane = reshape(F(:,iX,:),101,101);
cData = contourc(plane,[0 0]);
xData = iX.*ones(1,cData(2,1));
plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'r');
pause(0.1), drawnow
end
%描出X-Z平面的心形轮廓
for iY = 41:2:61
plane = reshape(F(iY,:,:),101,101);
cData = contourc(plane,[0 0]);
yData = iY.*ones(1,cData(2,1));
plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'r');
pause(.5), drawnow
end
%描出X-Y平面的心形轮廓
for iZ = 36:1:71
plane = F(:,:,iZ);
cData = contourc(plane,[0 0]);
startIndex = 1;
if size(cData,2) > (cData(2,1)+1)
startIndex = cData(2,1)+2;
zData = iZ.*ones(1,cData(2,1));
plot3(hAxes,cData(1,2:(startIndex-1)),...? ????
cData(2,2:(startIndex-1)),zData,'r');
end
zData = iZ.*ones(1,cData(2,startIndex));
plot3(hAxes,cData(1,(startIndex+1):end),...??????
cData(2,(startIndex+1):end),zData,'r');
pause(.1), drawnow
end
%给三维心着色补光
alpha(1) %恢复透明度
set(p,'facecolor','r','edgecolor','none');
camlight %补光
lighting gouraud
pause(.2)
%做线
line([20 80],[50 50],[52.5 2.5], 'color','r')
line([50 50],[20 80],[52.5 52.5], 'color','r')
line([50 50],[50 50],[30 80], 'color','r')
pause(0.5)
%文字部分;
text(0,50,107,'Surround you with my love\heartsuit','fontweight','bold','fontsize',25,'color','r');
pause(2)
text(7,100,70,['宝贝\heartsuit'], 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet?MS','fontsize',45,'Color','r');
pause(.5)
text(80,50,43,'YOU', 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet?MS','fontsize',60,'Color','r');
pause(.2)
text(75,5,40,'理工男的表白\heartsuit', 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet?MS','fontsize',20,'Color','r');
pause(.2)
text(120,0,20,'xys\heartsuit', 'fontWeight','bold','FontAngle','italic','FontName','Trebuchet?MS','fontsize',10,'Color','r');
pause(.2)
uicontrol(hFigure,'Style','Edit','Units','normalized','Position',[0,0.8,1,0.12],...
'Backgroundcolor','[0,1,1]','String','节日快乐','Fontsize',40,'Foregroundcolor','[1,0,0]');
pause(.5)
uicontrol(hFigure,'Style','Edit','Units','normalized','Position',[0,0.8,1,0.12],...
'Backgroundcolor','[0.3,0.75,0.93]','String','节日快乐','Fontsize',40,'Foregroundcolor','[1,0,0]');
pause(.5)
uicontrol(hFigure,'Style','Edit','Units','normalized','Position',[0,0.8,1,0.12],...
'Backgroundcolor','[1,0.5,0]','String','节日快乐','Fontsize',40,'Foregroundcolor','[1,0,0]');
pause(.5)
uicontrol(hFigure,'Style','Edit','Units','normalized','Position',[0,0.8,1,0.12],...
'Backgroundcolor','[1,1,1]','String','节日快乐','Fontsize',40,'Foregroundcolor','[1,0,0]');
pause(.5)
uicontrol(hFigure,'Style','Edit','Units','normalized','Position',[0,0.8,1,0.12],...
'Backgroundcolor','[1,1,0]','String','节日快乐','Fontsize',40,'Foregroundcolor','[1,0,0]');
toc
三、教你如何个性化定制
个性化定制1
修改第25行代码:可以把“宝贝”替换成任意词哦,比如她的名字、她的昵称……
修改的内容,对应在这里:
个性化定制2
修改第78行代码,也可以把'Surround you with my love \heartsuit'改成你想说的话哦~
(建议右边的\heartsuit不要改哦,它画出来的符号图案是心形♥哦)
修改的内容,对应位置在这里:
个性化定制3
修改第78行代码,也可以把“宝贝”改成她的名字、她的昵称……
(建议宝贝右边的\heartsuit不要改哦,它画出来的符号图案是心形♥哦)
修改的内容,对应位置在这里:
个性化定制4
修改第84行代码,也可以把'xys \heartsuit'改成你的名字的首字母哦~
(建议右边的\heartsuit不要改哦,它画出来的符号图案是心形♥哦)
修改的内容,对应位置在这里:
个性化定制5
修改第87、90、93、96、99行代码,也可以把`节日快乐‘修改成你想要送给她那天的节日哦!比如“520快乐”、“情人节快乐”、“七夕快乐”……
修改的内容,对应位置在这里:
好啦,类似的…其他的内容也可以根据自己的喜好和创意来修改,如果你是个matlab大神,有更好的动图创意,记得艾特我互相学习哈~
如有疑问,欢迎批评指正^^
Post Views:
8