在別人的博文里看到 Lissajous曲線的動(dòng)態(tài)圖, 感覺(jué)還挺帥,于是自己也想玩一下。
Lissajous曲線
在數(shù)學(xué)上,Lissajous曲線(又稱Lissajous圖形或Bowditch曲線)是由如下參數(shù)方程構(gòu)成的曲線族的圖形 [1]
![參數(shù)方程][Equation]
[Equation]: http://latex.codecogs.com/svg.latex?\begin{cases}x=A\sin(at+\varphi)\y=B\sin(bt)\end{cases}
納撒尼爾·鮑迪奇(Nathaniel Bowditch)在1815年首先研究了這一曲線族,之后在1857年由朱爾斯·安東尼·利薩如(Jules Antoine Lissajous)進(jìn)行了更詳細(xì)的研究。
在物理上,Lissajous曲線可以看作是一個(gè)質(zhì)點(diǎn)同時(shí)在X軸和Y軸方向上做簡(jiǎn)諧運(yùn)動(dòng)形成的運(yùn)動(dòng)軌跡(以我僅剩的一點(diǎn)物理知識(shí)來(lái)說(shuō),這大概是叫"運(yùn)動(dòng)的合成")。
當(dāng)這兩個(gè)相互垂直方向上的簡(jiǎn)諧振動(dòng)的頻率為任意值時(shí),那么它們合成的運(yùn)動(dòng)可能會(huì)比較復(fù)雜,軌跡是不穩(wěn)定的,示意圖如下。(關(guān)于穩(wěn)定,我的理解大概就是呈周期性吧)
而如果這兩個(gè)振動(dòng)的頻率成簡(jiǎn)單的整數(shù)比,就能合成一個(gè)規(guī)則的、穩(wěn)定的閉合曲線,這就是Lissajous曲線(示意圖如下)。
具體實(shí)現(xiàn)
- 環(huán)境: Matlab R2017a
Animated Line 實(shí)現(xiàn)動(dòng)畫(huà)效果
首先用animatedline
創(chuàng)建初始動(dòng)畫(huà)線條對(duì)象 [2] ,接著在循環(huán)中動(dòng)態(tài)地向線條中添加點(diǎn),并使用 drawnow
在屏幕上顯示該新添加的點(diǎn),然后用getframe
捕獲的當(dāng)前圖像。
%%
% Create a new figure window,
% Background color is white
% MenuBar is hidden
% Image size is 480*480
figure('Color','w','MenuBar','none','InnerPosition',[0 0 480 480]);
co = [65/255, 131/255, 196/255]; % specify the color
% Create animated line object
headMarker = animatedline('LineStyle','none','Marker','o','Color',co,'MarkerFaceColor',co);
body = animatedline('LineStyle','-','LineWidth',1.3,'Color',co); % trajectory
axis([-1,1,-1,1]); axis off; % hide axis
%%
% Parameters
a = 13;
b = 18;
phi = pi * 0;
% Parametric Equation of Lissajous Curve
t = linspace(0,2*pi,1000); % generate 1000 linearly equally spaced points
x = sin(a*t + phi);
y = sin(b*t);
%%
% Display
for idx = 1:length(t)
addpoints(headMarker,x(idx),y(idx));
addpoints(body,x(idx),y(idx));
drawnow
% Capture the current plot
frame = getframe; % snapshot of the current axes
im{idx} = frame2im(frame); % write image data to `im`
% Remove previous head marker except for the last one
if idx~=length(t)
clearpoints(headMarker);
end
end
寫(xiě)入GIF動(dòng)圖
將getframe
捕獲的每一個(gè)圖像,寫(xiě)入 GIF 動(dòng)畫(huà)文件 [3] 。
%%
% Save images into a GIF file.
% Because three-dimensional data is not supported for GIF files,
% call rgb2ind to convert the RGB data in the image to an indexed image A with a colormap map.
filename = 'Lissajous.gif'; % specify the output file name
for idx = 1:length(t)
[A,map] = rgb2ind(im{idx},256);
if idx == 1
% Set DelayTime = 0 to display next images as fast as your hardware allows
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0);
else
% To append multiple images to the first image
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0);
end
end
完整代碼鏈接
參考
- http://www.matrix67.com/blog/archives/6947
- https://codepen.io/handsomeone/full/Kgmbqg
- https://cn.mathworks.com/matlabcentral/fileexchange/28987-lissajous-curve
- https://cn.mathworks.com/examples/matlab/community/22655-lissajous-curves
- http://www.baike.com/wiki/%E5%88%A9%E8%90%A8%E5%A6%82%E5%9B%BE%E5%BD%A2