forked from mathworks/awesome-matlab-students
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Flapping_butterfly.m
69 lines (62 loc) · 2.06 KB
/
Flapping_butterfly.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
animateFrames();
function animateFrames()
animFilename = 'Flapping_butterfly.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame);
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, 'LoopCount', Inf, 'DelayTime', delayTime);
else
imwrite(A,map,animFilename, 'WriteMode', 'append', 'DelayTime', delayTime);
end
end
end
function drawframe(f)
% % sw: 48<=flapN<=1000;
flapN = 96;
n=20000;
phi=23/180*pi;
tM=70;
flap=linspace(0,4*pi,flapN)';
theta=tM/180*pi*sin(flap+(pi-asin(17/tM))*ones(flapN,1));
% % % plot(vx,vy,'b-','MarkerSize',0.5); axis off
vt=linspace(0,20*pi,n)';
vx=sin(vt).*(exp(cos(vt))-2*cos(4*vt)-(sin(vt/12)).^5)*cos(phi);
vy=cos(vt).*(exp(cos(vt))-2*cos(4*vt)-(sin(vt/12)).^5)*cos(phi);
% % % sw: My color scheme;
cA=[0.298,0.7647,1]; cB=[1,0.0196,0.4863];
% CM=(1-abs(vx)/max(vx))*cA+abs(vx)/max(vx)*cB;
CM=(1-0.5*(abs(vx)/max(vx)+abs(vy)/max(abs(vy))))*cA+0.5*(abs(vx)/max(vx)+abs(vy)/max(abs(vy)))*cB;
% % % sw: Calculate positions;
PT=zeros(n,3,length(flap));
for ti=1:length(flap)
PT(:,:,ti)=[zeros(n,1),vy,vy*sin(phi)]+[vx*cos(theta(ti)),abs(vx)*sin(theta(ti))*cos(pi/2+phi),abs(vx)*sin(theta(ti))*sin(pi/2+phi)];
end
% % % sw: Cuboid framework;
a=floor(max(abs(vx)))+1;
b=floor(max(vy))+1;
c=floor(min(vy));
% figure;
set(gcf,'color','black');
xlim([-a,a]); ylim([c,b]); zlim([-b,b]);
FW=[-a,c,-b; -a, b, -b; a, c, -b; -a, b, -b; -a,c,b; -a, b, b; a, c, b; -a, b, b];
% % % sw: Draw flapping butterfly;
% view(3); axis equal;
% figure(gcf);
clf;
tj=mod(f,flapN)+1;
scatter3(FW(:,1),FW(:,2),FW(:,3),0.1,[0,0,0],'.','MarkerFaceAlpha',0,'MarkerEdgeAlpha',0); hold on;
scatter3(PT(:,1,tj),PT(:,2,tj),PT(:,3,tj),1,CM,'.'); hold on;
grid off; axis off;
axis vis3d;
view(-30+360*ti/length(flap),39);
end