Привет, я выкладываю решение вопроса здесь.Спасибо всем, кто нашел время, чтобы взглянуть на это.
for k=2:length(x)
%%%Plot road
rl=-2; rr=0; w=4; l=100;
yroad = 0:l;
xroad = repmat(rl+w/2,l+1);
plot(xroad, yroad, 'w--','LineWidth',1.5);
hold on;
rectangle('Position',[rl,rr,w,l],'FaceColor',[0 0 0 0.9]);
axis([-4 4 0 50]);
hold on;
%%%%%
set(gca,'children',flipud(get(gca,'children')))
%%%time annotation
str = strcat('t=',string(t));
% annotation('textbox',[0.8 0.8 .5 .5],'String',str,'FitBoxToText','on');
text(1,47,str,'Color','w')
ratio = diff(get(gca, 'YLim'))/diff(get(gca, 'XLim'));
phi = atan2(y(k) - y(k - 1), (x(k) - x(k - 1))*ratio);
[x_rect_rot, y_rect_rot]=get_rectangle(phi, x(k), y(k));
plot(polyshape(x_rect_rot, y_rect_rot),'FaceColor','c','FaceAlpha',0.85);
hold on;
plot(x(1:k), y(1:k));
hold on;
drawnow;
end
function[xa,ya] =get_rectangle(phi,xc,yc,varargin)
if length(varargin)<2
h = 5;
w = 3;
else
h = varargin{4};
w = varargin{5};
end
x_rect = [-h, h, h, -h]/2;
y_rect = [-w, -w, w, w]/2;
% Consider aspect ratio of the axis
ratio = diff(get(gca, 'YLim'))/diff(get(gca, 'XLim'));
% Calculate rotated rectangle
x_rect_rot = x_rect*cos(phi) - y_rect*sin(phi);
y_rect_rot = x_rect*sin(phi) + y_rect*cos(phi);
% Incorporate ratio
x_rect_rot = x_rect_rot/ratio;
% Calculate offset
xa = x_rect_rot + xc;
ya = y_rect_rot + yc;
end