【问题标题】:How to plot and animate two internal tangent circles in MATLAB?如何在 MATLAB 中绘制两个内切圆并为其设置动画?
【发布时间】:2014-08-15 03:59:09
【问题描述】:

我想画两个从内部相切的圆,然后较小的一个(内部圆)在另一个表面上移动。我想在MATLAB创建这个函数,我的意思是画和移动圆圈:


(来源:uga.edu

我想在较大的表面上围绕另一个小圆圈移动较小的圆圈。

【问题讨论】:

标签: matlab animation


【解决方案1】:

你的意思是这样的吗?

function M = circles(steps)
    figure
    M(steps) = struct('cdata', [], 'colormap', []);
    for i=1:steps
        phi = i*2*pi/steps;
        two_circles(4, 1, phi);
        M(i) = getframe;
    end
end

function two_circles(r1, r2, phi)
    d = r1-r2;
    circle(r1, 0, 0);
    axis square
    hold on
    circle(r2, d*sin(phi), d*cos(phi));
    hold off
end

function circle(r, x0, y0)
    t = 0:.01:2*pi;
    plot(x0+r*sin(t), y0+r*cos(t));
end

这段代码绘制了两个圆,然后在其中一个在另一个的表面上移动时进行动画处理。

它还返回一个可以在movie函数中使用的帧数组:

M = circles(100);
movie(M, 10, 100);

或创建您自己的gif image 或视频文件。

这是一个从 frames 数组创建 gif 文件的示例:

function frames_to_gif( filename, frames, delay )
    first = true;
    for frame = frames
          im = frame2im(frame);
          [imind, cm] = rgb2ind(im, 256);
          if first
              first = false;
              imwrite(imind, cm, filename, 'gif',
                  'Loopcount', inf,
                  'DelayTime', delay);
          else
              imwrite(imind, cm, filename, 'gif',
                  'WriteMode', 'append',
                  'DelayTime', delay);
          end
    end
end

这是我用它创建的 gif 文件:

frames_to_gif('circles.gif', M, 1/200);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    相关资源
    最近更新 更多