# 長方形を動かす方法が知りたいです．

6 views (last 30 days)

Answered: Atsushi Ueno on 19 Jan 2023

t = linspace(0,50,200);
hold on
h1 = plot(y1(1) , 7.5 , ' ro ');
h2 = plot(7.5 , y2(1) , ' bo ');
h3 = plot(y3(1) , -7.5 , ' ro ');
h4 = plot(-7.5 , y4(1) , 'bo');
h5 = plot(y5(1) , 7.5 , 'ro');
h6 = plot(7.5 , y6(1) , 'bo');
h7 = plot(y7(1) , -7.5 , 'ro');
h8 = plot(-7.5 , y8(1) , 'bo');
axis([-400 400 -400 400])
grid on
for n = 1:length(t)
set(h1, 'XData', y1(n), 'YData', 7.5)
set(h2, 'XData', 7.5, 'YData', y2(n));
set(h3, 'XData', y3(n), 'YData', -7.5);
set(h4, 'XData', -7.5, 'YData', y4(n));
set(h5, 'XData', y5(n), 'YData', 7.5);
set(h6, 'XData', 7.5, 'YData', y6(n));
set(h7, 'XData', y7(n), 'YData', -7.5);
set(h8, 'XData', -7.5, 'YData', y8(n));
drawnow
end
xlabel('x[m]')
ylabel('y[m]')

Atsushi Ueno on 19 Jan 2023

rectplotは自作の関数です。一番下に書きました。
h1 = rectplot(y1(1) , 7.5 , ' ro ');
h2 = rectplot(7.5 , y2(1) , ' bo ');
h3 = rectplot(y3(1) , -7.5 , ' ro ');
h4 = rectplot(-7.5 , y4(1) , 'bo');
h5 = rectplot(y5(1) , 7.5 , 'ro');
h6 = rectplot(7.5 , y6(1) , 'bo');
h7 = rectplot(y7(1) , -7.5 , 'ro');
h8 = rectplot(-7.5 , y8(1) , 'bo');
set(h1,'Position',[y1(n),7.5 ,10, 10])
set(h2,'Position',[7.5 ,y2(n),10, 10]);
set(h3,'Position',[y3(n),-7.5 ,10, 10]);
set(h4,'Position',[-7.5 ,y4(n),10, 10]);
set(h5,'Position',[y5(n),7.5 ,10, 10]);
set(h6,'Position',[7.5 ,y6(n),10, 10]);
set(h7,'Position',[y7(n),-7.5 ,10, 10]);
set(h8,'Position',[-7.5 ,y8(n),10, 10]);

function h = rectplot(x,y,style)
cl = style(1); % 'ro'や'bo'の最初の一文字だけ色データとして貰う
h = rectangle('Position',[x,y,10,10],'EdgeColor',cl);
end

### Categories

Find more on Annotations in Help Center and File Exchange

R2022b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!