使矩形跟随轨迹
Make a rectangle follow a trajectory
我正在尝试让矩形的中心跟随轨迹并根据轨迹旋转它,就像这个视频中显示的那样(视频没有轨迹但显示了一个简单的旋转矩形) https://www.youtube.com/watch?v=NT45HE7L7yk。
到目前为止这是我的代码
figure;
subplot(2,1,1)
%visualize trajectory
ax = gca;
h = hgtransform('Parent',ax);
hold on
plot(x(1),y(1),'o','Parent',h);
for k = 2:length(x)
m = makehgtform('translate',x(k)-x(1),y(k)-y(1),0);
h.Matrix = m;
axis([2 10 2 10]);
drawnow
end
subplot(2,1,2)
ax = gca;
polyin = polyshape([0.1 0.1 -0.1 -0.1], [0.2 -0.2 -0.2 0.2]); % %w/2,h/2
% polyin = rectangle('Position',[-0.1 -0.1 0.2, 0.4]);
polyout = rotate(polyin, -atand(Y(1)/X(1)));
polyout = translate(polyout,[X(1) Y(1)]);
plot(polyout);
axis([2 10 2 10]);
for k=2:length(x)
hold on;
t = atan((y(k-1)-y(k))/(x(k)-x(k-1)))
% t = acos((y(k-1)*x(k-1)+y(k)*x(k))/(norm([x(k-1) y(k-1)])*norm([x(k) y(k)])));
% t = atand(y(k))/(x(k));
if (x(k)* y(k-1)- y(k)*x(k-1) )< 0
t = -1*t;
end
polyout=translate(polyout,x(k)-x(k-1),y(k)-y(k-1));
polyout=rotate(polyout, t);
plot(polyout);
axis([2 10 2 10]);
drawnow;
end
我希望矩形朝着轨迹中的下一个点旋转,并且中心沿着轨迹旋转,但是我的代码无法正常工作。
非常感谢您的帮助,感谢您的宝贵时间。
x和y数据供参考
x=[2 2.20138766511364 2.40315325924044 2.60463533746946 2.80522353545458 3.00435711210347 3.20152349226664 3.39625680942646 3.58813644838617 3.77678558795894 3.96186974365685 4.14309531037993 4.32020810510520 4.49299190957566 4.66126701298937 4.82488875468838 4.98374606684784 5.13776001716501 5.28688235154823 5.43109403680600 5.57040380333597 5.70484668781400 5.83448257588312 5.95939474484264 6.07968840633708 6.19548924904527 6.30694198136934 6.41420887412373 6.51746830322425 6.61691329237706 6.71275005576773 6.80519654075027 6.89448097053608 6.98084038688309 7.06451919278466 7.14576769515872 7.22484064753668 7.30199579275257 7.37749240563194 7.45158983568100 7.52454604977557 7.59661617485011 7.66805104058679 7.73909572210444 7.80998808264764 7.88095731627572 7.95222249055176 8.02399108923166 8.09645755495312 8.16980183192468 8.24418790861475 8.31976236044063 8.39665289245752 8.47496688204757 8.55478992160887 8.63618436124451 8.71918785145157 8.80381188581016 8.89004034367245 8.97782803285167 9.06709923231116 9.15774623485338 9.24962788980894 9.34256814572560 9.43635459305732 9.53073700685329 9.62542588944692 9.72009101314488 9.81435996291615 9.90781667908099 10]
y=[2 2.02032989606539 2.04061708413251 2.06134914940940 2.08296946958339 2.10587858004199 2.13043553909377 2.15695929318928 2.18573004214188 2.21699060434868 2.25094778201139 2.28777372635721 2.32760730285977 2.37055545645991 2.41669457678668 2.46607186337814 2.51870669090231 2.57459197437800 2.63369553439574 2.69596146233865 2.76131148560331 2.82964633282069 2.90084709907698 2.97477661113452 3.05128079265267 3.13019002940870 3.21132053451867 3.29447571365832 3.37944753028395 3.46601787085333 3.55395991004655 3.64303947598694 3.73301641546193 3.82364595914396 3.91468008681135 4.00586889256917 4.09696195007019 4.18770967773568 4.27786470397637 4.36718323241328 4.45542640709865 4.54236167773681 4.62776416490505 4.71141802527453 4.79311781683116 4.87266986409647 4.94989362334853 5.02462304784280 5.09670795303304 5.16601538179218 5.23243096963322 5.29586030993012 5.35623031913866 5.41349060201735 5.46761481684833 5.51860204065821 5.56647813443899 5.61129710836894 5.65314248703349 5.69212867464610 5.72840232026917 5.76214368303492 5.79356799736624 5.82292683819765 5.85050948619610 5.87664429298192 5.90170004634970 5.92608733548913 5.95025991620594 5.97471607614275 6]
既然你想让矩形围绕它的中心旋转,你应该指定旋转的参考点是矩形的中心。其次,在旋转时你必须记住,在上一步中你已经做了旋转,因此需要用新的角度减去之前的角度。
polyin = polyshape([0.1 0.1 -0.1 -0.1], [0.2 -0.2 -0.2 0.2]); % %w/2,h/2
% polyin = rectangle('Position',[-0.1 -0.1 0.2, 0.4]);
polyout = translate(polyout,[x(1) y(1)]);
plot(polyout);
axis([2 10 2 10]);
prev_t = 0;
for k=2:length(x)
hold on;
t = atan((y(k)-y(k-1))/(x(k)-x(k-1))) % corrected formula
% if (x(k)* y(k-1)- y(k)*x(k-1) )< 0 % commented because I don't see why this should be here
% t = -1*t;
% end
polyout=translate(polyout,x(k)-x(k-1),y(k)-y(k-1));
polyout=rotate(polyout, t - prev_t, [x(k) y(k)]);
plot(polyout);
axis([2 10 2 10]);
drawnow;
prev_t = t;
end
我正在尝试让矩形的中心跟随轨迹并根据轨迹旋转它,就像这个视频中显示的那样(视频没有轨迹但显示了一个简单的旋转矩形) https://www.youtube.com/watch?v=NT45HE7L7yk。
到目前为止这是我的代码
figure;
subplot(2,1,1)
%visualize trajectory
ax = gca;
h = hgtransform('Parent',ax);
hold on
plot(x(1),y(1),'o','Parent',h);
for k = 2:length(x)
m = makehgtform('translate',x(k)-x(1),y(k)-y(1),0);
h.Matrix = m;
axis([2 10 2 10]);
drawnow
end
subplot(2,1,2)
ax = gca;
polyin = polyshape([0.1 0.1 -0.1 -0.1], [0.2 -0.2 -0.2 0.2]); % %w/2,h/2
% polyin = rectangle('Position',[-0.1 -0.1 0.2, 0.4]);
polyout = rotate(polyin, -atand(Y(1)/X(1)));
polyout = translate(polyout,[X(1) Y(1)]);
plot(polyout);
axis([2 10 2 10]);
for k=2:length(x)
hold on;
t = atan((y(k-1)-y(k))/(x(k)-x(k-1)))
% t = acos((y(k-1)*x(k-1)+y(k)*x(k))/(norm([x(k-1) y(k-1)])*norm([x(k) y(k)])));
% t = atand(y(k))/(x(k));
if (x(k)* y(k-1)- y(k)*x(k-1) )< 0
t = -1*t;
end
polyout=translate(polyout,x(k)-x(k-1),y(k)-y(k-1));
polyout=rotate(polyout, t);
plot(polyout);
axis([2 10 2 10]);
drawnow;
end
我希望矩形朝着轨迹中的下一个点旋转,并且中心沿着轨迹旋转,但是我的代码无法正常工作。 非常感谢您的帮助,感谢您的宝贵时间。
x和y数据供参考
x=[2 2.20138766511364 2.40315325924044 2.60463533746946 2.80522353545458 3.00435711210347 3.20152349226664 3.39625680942646 3.58813644838617 3.77678558795894 3.96186974365685 4.14309531037993 4.32020810510520 4.49299190957566 4.66126701298937 4.82488875468838 4.98374606684784 5.13776001716501 5.28688235154823 5.43109403680600 5.57040380333597 5.70484668781400 5.83448257588312 5.95939474484264 6.07968840633708 6.19548924904527 6.30694198136934 6.41420887412373 6.51746830322425 6.61691329237706 6.71275005576773 6.80519654075027 6.89448097053608 6.98084038688309 7.06451919278466 7.14576769515872 7.22484064753668 7.30199579275257 7.37749240563194 7.45158983568100 7.52454604977557 7.59661617485011 7.66805104058679 7.73909572210444 7.80998808264764 7.88095731627572 7.95222249055176 8.02399108923166 8.09645755495312 8.16980183192468 8.24418790861475 8.31976236044063 8.39665289245752 8.47496688204757 8.55478992160887 8.63618436124451 8.71918785145157 8.80381188581016 8.89004034367245 8.97782803285167 9.06709923231116 9.15774623485338 9.24962788980894 9.34256814572560 9.43635459305732 9.53073700685329 9.62542588944692 9.72009101314488 9.81435996291615 9.90781667908099 10]
y=[2 2.02032989606539 2.04061708413251 2.06134914940940 2.08296946958339 2.10587858004199 2.13043553909377 2.15695929318928 2.18573004214188 2.21699060434868 2.25094778201139 2.28777372635721 2.32760730285977 2.37055545645991 2.41669457678668 2.46607186337814 2.51870669090231 2.57459197437800 2.63369553439574 2.69596146233865 2.76131148560331 2.82964633282069 2.90084709907698 2.97477661113452 3.05128079265267 3.13019002940870 3.21132053451867 3.29447571365832 3.37944753028395 3.46601787085333 3.55395991004655 3.64303947598694 3.73301641546193 3.82364595914396 3.91468008681135 4.00586889256917 4.09696195007019 4.18770967773568 4.27786470397637 4.36718323241328 4.45542640709865 4.54236167773681 4.62776416490505 4.71141802527453 4.79311781683116 4.87266986409647 4.94989362334853 5.02462304784280 5.09670795303304 5.16601538179218 5.23243096963322 5.29586030993012 5.35623031913866 5.41349060201735 5.46761481684833 5.51860204065821 5.56647813443899 5.61129710836894 5.65314248703349 5.69212867464610 5.72840232026917 5.76214368303492 5.79356799736624 5.82292683819765 5.85050948619610 5.87664429298192 5.90170004634970 5.92608733548913 5.95025991620594 5.97471607614275 6]
既然你想让矩形围绕它的中心旋转,你应该指定旋转的参考点是矩形的中心。其次,在旋转时你必须记住,在上一步中你已经做了旋转,因此需要用新的角度减去之前的角度。
polyin = polyshape([0.1 0.1 -0.1 -0.1], [0.2 -0.2 -0.2 0.2]); % %w/2,h/2
% polyin = rectangle('Position',[-0.1 -0.1 0.2, 0.4]);
polyout = translate(polyout,[x(1) y(1)]);
plot(polyout);
axis([2 10 2 10]);
prev_t = 0;
for k=2:length(x)
hold on;
t = atan((y(k)-y(k-1))/(x(k)-x(k-1))) % corrected formula
% if (x(k)* y(k-1)- y(k)*x(k-1) )< 0 % commented because I don't see why this should be here
% t = -1*t;
% end
polyout=translate(polyout,x(k)-x(k-1),y(k)-y(k-1));
polyout=rotate(polyout, t - prev_t, [x(k) y(k)]);
plot(polyout);
axis([2 10 2 10]);
drawnow;
prev_t = t;
end