以下は、2Dベジェ曲線をプロットするMATLAB関数です。私の目標は、バックグラウンドで使用されているさまざまなコンポーネントのすべてをアニメーション化することです。つまり、ベジェ曲線への接線、コントロールポイント間のポイントなどです。このビデオの終わりに向かってアニメーションと似たようなものがあります。 Cubic Bezier Curves - Under the Hood。MATLABのアニメーション - 複数の移動ポイントを同時にアニメーション化する
私の問題は、コントロールポイント間で同時に実行されているすべてのポイントをプロットしようとすると、かなり激しいちらつきが発生することです。
私は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
を確認する方が簡単な場合があり、同時に全ての線分上の赤の点をプロットすると、ループのためにその内部ではなく、考え、既存の*プロットオブジェクトの 'YData'プロパティ。 – Suever
@ Suver私はこれを私の関数に実装する方法が完全にはわかりませんが、あなたが意味することの例を教えてくれますか? – Will