2017-01-11 78 views
1

以下は、2Dベジェ曲線をプロットするMATLAB関数です。私の目標は、バックグラウンドで使用されているさまざまなコンポーネントのすべてをアニメーション化することです。つまり、ベジェ曲線への接線、コントロールポイント間のポイントなどです。このビデオの終わりに向かってアニメーションと似たようなものがあります。 Cubic Bezier Curves - Under the HoodMATLABのアニメーション - 複数の移動ポイントを同時にアニメーション化する

私の問題は、コントロールポイント間で同時に実行されているすべてのポイントをプロットしようとすると、かなり激しいちらつきが発生することです。

私はMATLABを使用したアニメーションにはかなり新しく、アニメーション用の多くのコードはさまざまなソースから取得されています。どんな助けも素晴らしいだろう!

P.S.私が使用しているコントロールポイントは[1 1;2 6;7 7;10 2]です。

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 
+0

を確認する方が簡単な場合があり、同時に全ての線分上の赤の点をプロットすると、ループのためにその内部ではなく、考え、既存の*プロットオブジェクトの 'YData'プロパティ。 – Suever

+0

@ Suver私はこれを私の関数に実装する方法が完全にはわかりませんが、あなたが意味することの例を教えてくれますか? – Will

答えて

3

あなたの最後のネストされたforループの内側ではなく、既存のプロットのオブジェクトを更新し、新たなプロットオブジェクトを作成しているので、あなたは可能性が低下した性能を得ています。あなたはまた、あなたはちらつきを減らすために'on'にフィギュアセットのDoubleBuffer性質を持っていることを確認することができます

% 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 

代わりに、既存のプロットオブジェクトの XDataYDataプロパティを変更するためにそのループを書き換えることができます。

set(gcf, 'DoubleBuffer', 'on') 

はまた、私が考える「ちらつき」実際にはMATLABは、非常に高速な赤い点の移動をレンダリングしている(と、それは線分間をジャンプです)、あなたがちらつきとしてそれを見ているということかもしれません。私はむしろ新しいプロットオブジェクトを作成するよりも、私は `XData`を更新するだけでお勧めしたい

% 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 
+0

あなたの答えをありがとう。残念ながら、依然としてかなりちらつきがあります。 – Will

+0

@Willいくつかの提案を含むアップデートをご覧ください – Suever

+0

ありがとうございました!私の唯一の仕事は、ベジェ曲線と制御点の間の線、つまり提供されたリンクのビデオのピンクの線を結ぶ線をプロットすることです。それをどうやって行うのか考えていますか? – Will

関連する問題