我编写的代码模拟了三角形的滚动,并将输出作为三个顶点的跟踪图和指定转数的质心。
我想将此绘图转换为动画,并在每个三角形顶点处添加一个圆来表示轮子。
有人能建议一种有效的方法吗?
我希望制作一个像这样的动画,除了在我的情况下,我没有使用规则行为,只需要一个动画,而不是交互式的东西: http://demonstrations.wolfram.com/ARollingReuleauxTriangle/
close all
clear
%functions to create rigid body transform matrix---------------------------
hat = @(w) [0,-w(3),w(2); w(3),0,-w(1); -w(2),w(1),0];
rotMatrix = @(th,w) eye(3) + hat(w)*sind(th) + hat(w)*hat(w)*(1-cosd(th));
rbTrans = @(th,w,q) [rotMatrix(th,w), (eye(3) - rotMatrix(th,w))*q(1:3); 0 0 0 1];
%arguments: th=rotation angle, w=rotation axis, q=center of rotation (column vector)
nTurns=5;
degPerTurn=120; %degrees
nStepsPerTurn=1000; %resolution of each "turn". number of points
d = 5.2; %distance from centroid to a vertex
r = 4;
w = [0;0;1;0]; %rotation vector=z-axis
X0 = [0;0;0]; %initial position of centroid
%initialize vertices
v=[0;1;0];
p1 = X0 + [0;d;0];
p2 = X0 + d*rotMatrix(-120,w)*v;
p3 = X0 + d*rotMatrix(120,w)*v;
P(1,1,:) = p1;
P(2,1,:) = p2;
P(3,1,:) = p3;
P(4,1,:) = X0;
rotVertex=2;
thStep=-degPerTurn/nStepsPerTurn;
for i=1:nTurns
for j=1:nStepsPerTurn
%center of rotation
q = squeeze(P(rotVertex,end,:));
q = q + [0; -r; 0];
%transform is a rotation of 120/nSteps about z-axis centered at a
%particular vertex
g = rbTrans(thStep, w, q);
I=size(P,2)+1; %add new element by inserting to (end+1)
for k=1:size(P,1) %for each point we're following-- vertices and centroid
pt = g*[squeeze(P(k,I-1,:)); 1]; %apply transform to last existing value of this point
P(k,I,:) = pt(1:3); %and add it to the end
end
end
%vertices are numbered clockwise around the triangle
rotVertex=rotVertex-1;
if rotVertex<1
rotVertex=rotVertex+3;
end
end
%extract data from 3D array
P1=squeeze(P(1,:,:));
P2=squeeze(P(2,:,:));
P3=squeeze(P(3,:,:));
C=squeeze(P(4,:,:));
figure
plot(P1(:,1),P1(:,2),'b'),hold on
plot(P2(:,1),P2(:,2),'r')
plot(P3(:,1),P3(:,2),'g')
plot(C(:,1),C(:,2),'k')
xlabel('Horizontal Distance (inches)')
ylabel('Vertical Distance (inches)')
axis equal
legend('Vertex 1','Vertex 2','Vertex 3','Centroid', 'Location','Best')
figure
subplot(4,1,1),plot(P1(:,1),P1(:,2)),axis equal,ylabel('P1')
subplot(4,1,2),plot(P2(:,1),P2(:,2)),axis equal,ylabel('P2')
subplot(4,1,3),plot(P3(:,1),P3(:,2)),axis equal,ylabel('P3')
subplot(4,1,4),plot(C(:,1),C(:,2)),axis equal,ylabel('C')
xlabel('Horizontal Distance (inches)')
figure
plot(P3(:,1),P3(:,2),'b'),hold on
plot(C(:,1),C(:,2),'k')
axis equal
xlabel('Horizontal Distance (inches)')
ylabel('Vertical Distance (inches)')
legend('Vertex 3','Centroid', 'Location','Best')
答案 0 :(得分:0)
我会将你的点的绘制封装在一个for
循环中,单独绘制每个三个点,然后放置一个pause
命令几毫秒并伴随drawnow
。如果你想让它成为一个动画,我假设你只想在每个“帧”上显示一个三角形,那么你可以做的就是当你想绘制下一组点时,取出前面的点并绘制它们白色,人为地清除它们而不清除整个框架。
做这样的事情。请记住,我只针对我认为你想要动画发生的代码:
figure;
for i = 1 : size(P1, 1)
if (i > 1)
plot(P1(i-1,1), P1(i-1,2), 'w.', P2(i-1,1), P2(i-1,2), 'w.', ...
P3(i-1,1), P3(i-1,2), 'w.', C(i-1,1), C(i-1,2), 'w.');
end
plot(P1(i,1), P1(i,2), 'b.', P2(i,1), P2(i,2), 'r.', ...
P3(i,1), P3(i,2), 'g.', C(i,1), C(i,2), 'k.');
axis equal;
xlabel('Horizontal Distance (inches)');
ylabel('Vertical Distance (inches)');
legend('Vertex 1','Vertex 2','Vertex 3','Centroid', 'Location','Best');
drawnow;
pause(0.1); %// Pause for 0.1 seconds
end