MATLAB: Moving a Polygon on a Polygon

polygonpolyshape

Hello,
I created following Polygons on top of each other:
pgon = polyshape([440 0 0 440], [0 0 5.1 5.1]);
pgon2 = polyshape([18 0 0 18], [0 0 0.5 0.5]);
plot([pgon pgon2]);
Now I want that the smaller polygon moves from on side to the other and over and over again and moves up a little each time.
Is this possible. And if yes how?

Best Answer

pgon = polyshape([440 0 0 440], [0 0 5.1 5.1]);
path = [1:5:100 100:-5:1] ;
for i = path
pgon2 = polyshape([18 0 0 18]+i, [0 0 0.5 0.5]);
plot([pgon pgon2]);
drawnow
end