在別人的博文里看到 Lissajous曲線的動態圖, 感覺還挺帥,于是自己也想玩一下。
最終效果
Lissajous曲線
在數學上,Lissajous曲線(又稱Lissajous圖形或Bowditch曲線)是由如下參數方程構成的曲線族的圖形 [1]
![參數方程][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)進行了更詳細的研究。
在物理上,Lissajous曲線可以看作是一個質點同時在X軸和Y軸方向上做簡諧運動形成的運動軌跡(以我僅剩的一點物理知識來說,這大概是叫"運動的合成")。
當這兩個相互垂直方向上的簡諧振動的頻率為任意值時,那么它們合成的運動可能會比較復雜,軌跡是不穩定的,示意圖如下。(關于穩定,我的理解大概就是呈周期性吧)
一個示意圖
而如果這兩個振動的頻率成簡單的整數比,就能合成一個規則的、穩定的閉合曲線,這就是Lissajous曲線(示意圖如下)。
另一個示意圖
具體實現
- 環境: Matlab R2017a
Animated Line 實現動畫效果
首先用animatedline
創建初始動畫線條對象 [2] ,接著在循環中動態地向線條中添加點,并使用 drawnow
在屏幕上顯示該新添加的點,然后用getframe
捕獲的當前圖像。
%%
% 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
寫入GIF動圖
將getframe
捕獲的每一個圖像,寫入 GIF 動畫文件 [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