一年一度的 5.20 又快要到了
你还在为写情书却不知如何下笔而心烦意乱吗?
你还在因不知如何讨好女神欢心而郁郁寡欢吗?
你还在因被冠以“ 理工男” 的头衔,被骂 ”不懂浪漫“ 、”直男“ 而愤愤不平吗?
现在,不要998,不要98,也不要9.8,只需几行代码搞定ta
当然,如果用代码也被骂,那就老老实实送花
虽然我们是理工男,但是我们也懂浪漫啊
虽然对浪漫的理解可能有点偏差
废话不多说,直接上干货
1.使用Matlab绘制心形图
1.1 基础篇
只需一行代码,绘制心形的隐函数图像:
ezplot('x^2+(y-(x^2)^(1/3))^2=9')
稍微长一点的分段函数:
x=linspace(-2,2,1000);
y1=sqrt(2*sqrt(x.^2)-x.^2);
y2=-2.14*sqrt(sqrt(2)-sqrt(abs(x)));
plot(x,y1,'b',x,y2,'b');
axis([-2.5,2.5,-3,1.5]);
或者这种分段的三角函数:
t=linspace(-6,6,1000);
x=16*(sin(t)).^3;
y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t);
plot(x,y);
1.2 进阶篇
一个二维的心形平面图怎么能足以代表我们的诚意呢,最起码得个三维的心形吧。
f=@(x,y,z) x.^2.*z.^3+9*y.^2.*z.^3/80-(x.^2+9*y.^2/4+z.^2-1).^3; %心形曲面函数
[x,y,z]=meshgrid(-1.5:0.1:1.5); %画图范围
v=f(x,y,z);
%画图
h=patch(isosurface(x,y,z,v,0));
isonormals(x,y,z,v,h)
set(h,'FaceColor','r','EdgeColor','none');
title('heart beat')
alpha(0.8) %调整心形的透明度,越小越透明
grid on; %网格线开关
axis([-1.5 1.5 -1.5 1.5 -1.5 1.5])
lighting gouraud %光照设置
h = camlight('left');
for i = 1:180 %水平旋转照相机
camorbit(1,0)
camlight(h,'left');
drawnow;
end
1.3 高级篇
想要制作更复杂的效果?来点动画怎么样
请接着往下看。
上述动图完整的显示了三维心形的绘制过程,并且还有一段动画的实现。原作者的源代码里有详细的使用说明以及注释,我这里就不做过多说明了。原作者一共编写了三个函数文件,分别如下。运行时,须将三个函数文件放在同一路径中,运行demoDisplay.m
文件即可。
function demoDisplay
% This demo displays the generation of heart and also the possible re-shape and movement
% by Xin Zhao
% Feb 14, 2008
clear;
close all;
[x,y,z] = heart;
bottomZ = min(z(:));
radius = abs(bottomZ);
axis vis3d
axis off
daspect([1.6, 1, 1.875]);
hold on;
markerColor = [0.5, 0.5, 0.5];
lineColor = [0.7, 0.7, 0.7];
figWidth = 800;
figHeight = 600;
screenSize = get(0, 'ScreenSize');
X0 = (screenSize(3)-figWidth)/2;
Y0 = (screenSize(4)-figHeight)/2;
set(gcf, 'Position', [X0,Y0, figWidth, figHeight]);
camtarget([0, 0, 0]);
set(gcf,'Renderer','zbuffer');
set(gcf,'DoubleBuffer','on');
% RGB(255 105 180) is for pink color
set(gcf, 'color', [255 105 180]/255);
startAZ = 30;
satrtEL = 20;
view(startAZ, satrtEL);
% draw points
for iLoop = 1:size(x, 1)
plot3(x(iLoop, :), y(iLoop,:), z(iLoop, :), 'Marker', '.', 'MarkerEdgeColor', markerColor, 'LineStyle', 'none');
pause(0.2);
end
% draw wire frames
for iLoop = 1:size(x, 1)
plot3(x(iLoop, :), y(iLoop,:), z(iLoop, :), 'color', lineColor, 'LineStyle', '-');
pause(0.2);
end
for iLoop = 1:size(x, 2)
plot3(x(:, iLoop), y(:, iLoop), z(:, iLoop), 'color', lineColor, 'LineStyle', '-');
pause(0.2);
end
% rotate wire frame
for i=1:36
camorbit(10,0,'data');
pause(0.1);
end
% clean the curent axes
delete(get(gca, 'Children'));
camlight left;
newLineColor = [0.7, 0.4, 0.4];
% re-draw the whole heart with surf function
h = surf(x,y,z, 'EdgeColor', newLineColor, 'FaceColor', 'r');
for i=1:36
camorbit(10,0,'data');
pause(0.1);
end
mediumColor = [0.8, 0.3, 0.3];
set(h, 'EdgeColor', mediumColor,'FaceLighting','gouraud');
for i=1:36
camorbit(10,0,'data');
drawnow;
end
set(h, 'EdgeColor', 'none');
drawnow;
% define the Z-path for heart re-shape
deltaZ = linspace(-.3, 0, 5);
deltaZ = [fliplr(deltaZ), deltaZ];
deltaZ = repmat(deltaZ, 1, 3);
zlim('manual');
for iLoop = 1:length(deltaZ)
% draw the bouncing heart
curDeltaZ = deltaZ(mod(iLoop,length(deltaZ))+1);
ratio = (radius+curDeltaZ)*radius;
newZ = z*ratio + curDeltaZ;
if exist('h', 'var')&& ishandle(h), delete(h);end
h = surf(x,y, newZ, 'EdgeColor', 'none', 'FaceColor', 'r','FaceLighting','gouraud');
camorbit(-1, 0, 'data');
drawnow;
end
% draw the bouncing heart
deltaZ = generateSimulatedBouncingPath;
zlim([-1, 3]);
for iLoop = 1:length(deltaZ)
if exist('h', 'var')&& ishandle(h), delete(h);end
curDeltaZ = deltaZ(iLoop);
if curDeltaZ < 0
% when deltaZ is under the zero bar, it's hitting the ground
ratio = (radius+curDeltaZ)*radius;
newZ = z*ratio + curDeltaZ;
else
% when deltaZ is bigger than zero, it's a free object
newZ = z + curDeltaZ;
end
h = surf(x, y, newZ, 'EdgeColor', 'none', 'FaceColor', 'r','FaceLighting','gouraud');
% zoom in and out for more vivid effect
if iLoop <= length(deltaZ)/2
camzoom(0.99);
else
camzoom(1.005);
end
camorbit(390/length(deltaZ), 0, 'data');
drawnow;
end
function z = generateSimulatedBouncingPath(iterationNum, roundTimes)
% This function generates the Z-coordinate path for the center of heart.
% by Xin Zhao
% Feb 14, 2008
% define the z path of the center of ball for rountTimes of bouncing along z-axis
if nargin < 2
roundTimes = 3;
end
if nargin < 1
iterationNum = 40;
end
radius = 1;
deltaH = 1.5; % the highest point comapred with radius
deltaL = -0.3;% the lowest point comapred with radius
rangeY = [-2, 1];
% gravity accelerator constant
% Note: this is smaller to the real 9.8 value, but this makes the bouncing slower
g = 5;
% define the accelerator for the z < 0
% k is similar to spring coefficient, but it's more complicated than that.
% k just needs to satisfy one condition here, i.e. it will stop at location defined by
% deltaL*radius
k = g * deltaH/(-deltaL);
% start point
startZ = deltaH*radius;
% speed when z =0
V0 = sqrt(2*startZ*g);
% calcualte the total time for rountTimes of bouncing
% time for z > 0
plusZtime = sqrt(2*deltaH*radius/g);
% time for z < 0
minusZtime = sqrt(-2*deltaL*radius/k);
% half period
qPeriod = plusZtime + minusZtime;
% define totalTime
qPeriodTime = linspace(0, qPeriod, iterationNum);
% z path when z > 0
zPlus = startZ - 0.5 * g * qPeriodTime(qPeriodTime<plusZtime).^2;
% z path when z < 0
zMinus = -V0 * (qPeriodTime(qPeriodTime >=plusZtime)-plusZtime) + 0.5 * k* (qPeriodTime(qPeriodTime >=plusZtime)-plusZtime).^2;
z = [zPlus, zMinus, fliplr(zMinus), fliplr(zPlus)];
% the last zPlus is for the stop action
z = [repmat(z, 1, roundTimes), zPlus];
y = linspace(rangeY(1), rangeY(2), numel(z));
end
function [cordX, cordY, cordZ] = heart(sizeTheta, sizeFai)
% HEART function generates the x,y,z coordinates for the heart shape.
% it's usage is quite similar to the SPHERE function in Matlab, but do not draw anything.
% the default size of x,y,z coordinates is [30, 160]
% the parameter of sizeFai is actually a quater of the whole range of Fai.
% the mathematical equation used here is found on web page
% http://mathworld.wolfram.com/HeartSurface.html
% This program is for free, i.e. you can copy, modify or distribute it for any usage.
% And I perfer you use it to express love to your special one :-)
% by Xin Zhao
% Feb 14, 2008
if nargin ==0
sizeTheta = 30;
sizeFai = 40;
elseif nargin < 2
error('heart:NotEnoughArugments', 'Please give at least two input arugments');
end
theta = linspace(0, pi, sizeTheta)';
nudge = 0.0001; % used to avoid the operlapping
fai = linspace(0 + nudge, pi/2 - nudge, round(sizeFai/4));
a = 9/4;
b = 9/80;
A = 1+(a-1)*(sin(theta).^2) * (sin(fai).^2);
B = (sin(theta).^2.*cos(theta).^3) * (1 + (b-1)*(sin(fai).^2));
rou = zeros(size(A));
for iLoop = 1:numel(A)
curA = A(iLoop);
curB = B(iLoop);
% this is the polar coordinates version of the sextic equation found on
% http://mathworld.wolfram.com/HeartSurface.html
polyFactors = [curA^3, -curB, -3*curA^2, 0, 3*curA, 0, -1];
solutions = roots(polyFactors);
realRou = real(solutions(abs(imag(solutions))< 1e-9));
rou(iLoop) = realRou(realRou>0);
end
% x,y,z for the quater of whole heart
x = rou .* (sin(theta) * cos(fai));
y = rou .* (sin(theta) * sin(fai));
z = rou .* (cos(theta) * ones(size(fai)));
% x,y,z for the whole heart
cordX = [x, -fliplr(x) , -x, fliplr(x)];
cordY = [y, fliplr(y) , -y, -fliplr(y)];
cordZ = [z, fliplr(z), z, fliplr(z)];
参考链接[1]:https://blog.csdn.net/qq_39521554/article/details/79884849
参考链接[2]:https://www.ilovematlab.cn/forum.php?mod=viewthread&tid=303845
参考链接[3]:https://ww2.mathworks.cn/matlabcentral/fileexchange?q=Heart+Model+for+Valentine's+Day
2.建立表白网页
”网页大法“ 虽然也不错,但是需要购买服务器建立网站,最好还要买个域名。然而建网站本身就是一件很麻烦的事,对于这种一年用不几次的活动并不划算。当然,如果你已经有一个网站或者愿意折腾的,那么请接着往下看。
2.1 步骤
- 将我分享的源代码下载并解压。进入文件夹,随便选择一个模板文件(模板都存在以1、2、3等数字命名的文件夹中)
- 在你选择的模板文件中应该会看到一个
.html
文件。用浏览器打开就可以看见网页的效果。 - 修改表白网页的源代码,将
.html
用记事本或者其他软件打开,一般是修改里面的文字,改成你想说的话。或者是将模板文件夹中子文件夹里的图片改成你自己想要的图片,音乐改成你想要的音乐。改了之后要去.html
文件的相应位置做修改(一般是图片的名称或者音乐的名称发生了变化,不修改会产生引用错误) - 在网站的根目录(一般是与域名同名的一个目录)新建一个文件夹,将修改好的文件整个上传至网站新建的文件夹中(注意:需要上传修改后的以数字命名的文件夹里的所有文件,包括css、js、music等文件夹)
- 访问链接即可访问到上传到网站的表白网页了。比如我在网站的根目录下新建了一个名为 love 的文件夹,然后将我修改好的网页和相应的css、js等文件夹上传到了 love 文件夹中。我将
xx.html
文件重命名为love520.html
,那么访问链接 https://geomatlab.com/love/love520.html 就可以访问你上传的网页了。 - 将链接发给她/他,试试效果吧。
2.2 源代码
百度网盘链接:
链接:https://pan.baidu.com/s/18adRMlUqKh93GKy8Rt8mKQ
提取码:elbd
如果链接挂了或者有什么问题,可以在下方或者留言板留言。