Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MATLAB中的动画-同时设置多个移动点的动画_Matlab_Animation_Matlab Figure - Fatal编程技术网

MATLAB中的动画-同时设置多个移动点的动画

MATLAB中的动画-同时设置多个移动点的动画,matlab,animation,matlab-figure,Matlab,Animation,Matlab Figure,下面是绘制二维贝塞尔曲线的MATLAB函数。我的目标是为背景中使用的所有不同组件制作动画,即与贝塞尔曲线的切线、控制点之间的点等。我希望在本视频结尾处有一些类似于动画的内容: 我的问题是,当我试图同时绘制控制点之间运行的所有点时,会出现相当严重的闪烁 我对MATLAB的动画相当陌生,很多动画代码都是从不同的源代码中提取出来的。任何帮助都会很好 另外,我使用的控制点是[11;26;7;102] function bezier_anim(coords) % Coords to be entered

下面是绘制二维贝塞尔曲线的MATLAB函数。我的目标是为背景中使用的所有不同组件制作动画,即与贝塞尔曲线的切线、控制点之间的点等。我希望在本视频结尾处有一些类似于动画的内容:

我的问题是,当我试图同时绘制控制点之间运行的所有点时,会出现相当严重的闪烁

我对MATLAB的动画相当陌生,很多动画代码都是从不同的源代码中提取出来的。任何帮助都会很好

另外,我使用的控制点是
[11;26;7;102]

function bezier_anim(coords) % Coords to be entered in the format [x1 y1;x2 y2;...;xn yn]

close all

n = length(coords(:,1)); % Number of control points

syms t;
syms p;
B = zeros(2,1); % Bezier function

for i = 0:n-1
    % Equation for Bezier curve
    B = B + nchoosek(n-1,i) .* (1-t).^(n-1-i) .* t^i .* coords(i+1,:).'; 
end

for i = 1:n
   % Plot and label P_i
   x=coords(i,1);
   y=coords(i,2);
   plot(x,y,'kx')
   txt1 = '$$\mathbf{P}_';
   txt2 = num2str(i-1);
   txt3 = '$$';
   txt = [txt1 txt2 txt3];
   text(x,y,txt,'Interpreter','latex','VerticalAlignment','bottom','HorizontalAlignment','center')
   hold on
end

plot(coords(:,1),coords(:,2),'k--') % Plot lines between control points

L = sym('t',[2 n-1]); % Vector where eqs of lines are to be added

for i = 1:n-1 
    % Parametric equations of the straight lines between the control
    % points, for p between 0 and 1
    L(1,i) = (1-p)*coords(i,1) + p*coords(i+1,1);
    L(2,i) = (1-p)*coords(i,2) + p*coords(i+1,2);
end

% Animation of Bezier curve
g = animatedline;
x = matlabFunction(B(1));
y = matlabFunction(B(2));

for t = 0:0.01:1
    l = subs(L,'p',t); % Substitute current t value into eq for the lines
    addpoints(g,x(t),y(t));
    for k = 1:length(l(1,:)) % Plot all points running along each line simultaneously
        h(k) = plot(l(1,k),l(2,k),'r.');
        drawnow
        delete(h(k)) % Delete current point after it has been drawn 
                     % so there is not a trail
    end
    drawnow
end

end

性能可能会下降,因为您正在上次嵌套的
for
循环中创建新的打印对象,而不是更新现有的打印对象。您可以重写该循环以修改现有打印对象的属性

% Animation of Bezier curve

% Create the plot object to use later
hplot = plot(NaN, NaN, 'r.');

for t = 0:0.01:1
    l = subs(L,'p',t); % Substitute current t value into eq for the lines
    addpoints(g,x(t),y(t));

    for k = 1:length(l(1,:)) % Plot all points running along each line simultaneously
        % Update the position of the existing plot object
        set(hplot, 'XData', l(1,k), 'YData', l(2,k))
        drawnow
    end
    drawnow
end
您还可以确保将地物的
DoubleBuffer
属性设置为
'on'
,以减少闪烁

set(gcf, 'DoubleBuffer', 'on')
另外,我认为“闪烁”实际上可能只是因为MATLAB渲染红点的移动非常快(它在线段之间跳跃),而您看到它在闪烁。我认为,与内部for循环相比,在所有线段上同时绘制红色点可能更容易看到

% Create the plot object to use later
hplot = plot(NaN, NaN, 'ro');

for t = 0:0.01:1
    l = subs(L,'p',t); % Substitute current t value into eq for the lines
    set(hplot, 'XData', l(1,:), 'YData', l(2,:));

    addpoints(g,x(t),y(t));

    drawnow
end

与其创建新的绘图对象,我建议只更新现有绘图对象的
XData
YData
属性。@Suever我不完全确定如何在我的函数中实现这一点,你能给我一个例子说明你的意思吗?谢谢你的回答。不幸的是,仍然有相当严重的闪烁。谢谢!我现在唯一的任务是绘制连接贝塞尔曲线的线和控制点之间的线,即所提供链接中视频中的粉色线。你对如何做到这一点有什么想法吗?@Will这是特定于你的实现的,超出了StackOverflow的范围。