代码我没有注释,随手写的,如果有人看不懂可以问我(应该没有吧哈哈哈)
clc clear close all x1=60;y1=0;x2=400;y2=40;X_shudu=4;Y_shudu=6; while x2< 600 x2=x2+X_shudu; y2=y2-Y_shudu; P1=[x2 y2; x2 y2+20; x2+20 y2+20; x2+20 y2; NaN NaN; x1 y1; x1 y1+20; x1+100 y1+20; x1+100 y1;]; polyin1 =polyshape(P1); plot(polyin1) axis([0 600 0 400]); drawnow; if (x2>580) X_shudu=-X_shudu; end if y2>380 Y_shudu=-Y_shudu; end if x2<0 X_shudu=-X_shudu; end if y2<0 Y_shudu=-Y_shudu; x2=400;y2=40; end if abs(x2-x1)>10 x1=x2-10; end if ((x1<x2)&&(x2<(x1+100))&&(y2<(y1+20))&&(x2==(x1)))||((x1<(x2+20))&&((x2+20)<(x1+100))&&(y2<(y1+20)))||((x1<x2)&&(x2<(x1+100))&&(y2<(y1+20)))&&((x1<(x2+20))&&((x2+20)<(x1+100))&&(y2<(y1+20))) Y_shudu=-Y_shudu; end end