Lissajous曲线动画演示(Matlab实现)

来源:互联网 发布:淘宝客推广海报 编辑:程序博客网 时间:2024/05/21 09:04

在别人的博文里看到 Lissajous曲线的动态图, 感觉还挺帅,于是自己也想玩一下。


最终效果

最终效果


Lissajous曲线

在数学上,Lissajous曲线 [1] (又称Lissajous图形或Bowditch曲线)是由如下参数方程构成的曲线族的图形

{x=Asin(at+φ)y=Bsin(bt)

纳撒尼尔·鲍迪奇(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*480figure('Color','w','MenuBar','none','InnerPosition',[0 0 480 480]);co = [65/255, 131/255, 196/255]; % specify the color% Create animated line objectheadMarker = animatedline('LineStyle','none','Marker','o','Color',co,'MarkerFaceColor',co);body = animatedline('LineStyle','-','LineWidth',1.3,'Color',co); % trajectoryaxis([-1,1,-1,1]); axis off; % hide axis%% % Parametersa = 13;b = 18;phi = pi * 0;% Parametric Equation of Lissajous Curvet = linspace(0,2*pi,1000); % generate 1000 linearly equally spaced pointsx = sin(a*t + phi);y = sin(b*t);%%% Displayfor 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);    endend

写入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 namefor 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);    endend

完整代码链接

参考

  1. http://www.matrix67.com/blog/archives/6947
  2. https://codepen.io/handsomeone/full/Kgmbqg
  3. https://cn.mathworks.com/matlabcentral/fileexchange/28987-lissajous-curve
  4. https://cn.mathworks.com/examples/matlab/community/22655-lissajous-curves
  5. http://www.baike.com/wiki/%E5%88%A9%E8%90%A8%E5%A6%82%E5%9B%BE%E5%BD%A2

本文作者: 小路
本文链接: http://tinyland.site/posts/2017-07-02-matlab-implementation-for-lissajous-curve-animation-demo.html

原创粉丝点击