Animating the movement of a figure

조회 수: 9 (최근 30일)
Iñigo de la Joya
Iñigo de la Joya 2019년 12월 12일
편집: Andreas Bernatzky 2019년 12월 18일
I have written a code which makes makes a rectangle rotate about a certain point bya certain angle anticlockwise, but when I proceed on the animation, it draws every single rectangle as it is rotated. How can I make the rectangle rotate without leaving a trace after every iteration?
Here is the code which inolves some funtions:
function [colouredshape] = filledshape(shape,c)
% This function will draw a shape that is filled of a certain colour
fill(shape(1,:),shape(2,:),c);
end
%%%%%%%%%%%%%%%%%%%
function [finalshape] = rotateabout(shape,a,x,y)
% this function will rotate a shape anticlockwise about a certain point rather than the
% origin (angle a, coordinates (x,y))
[newshape] = translate(-x,-y,shape);
[rotatedshape] = rotate(newshape,a);
[finalshape] = translate(x,y,rotatedshape);
end
% This is animation
n = 10;
rectangle1 = [10 10 11 11 10; 1 5 5 1 1];
for a = linspace(0,pi/2,n)
[finalshape] = rotateabout(rectangle1,a,10,5);
filledshape(finalshape,'b')
drawnow
pause(0.1)
end

답변 (1개)

Andreas Bernatzky
Andreas Bernatzky 2019년 12월 13일
편집: Andreas Bernatzky 2019년 12월 13일
A rough and quick example:
for(n=1:1:360)
RectangleData = calcRectangle(n); %calculate your new position
hRectangle = plot(RectangleData); % plot your rectangle
% Now get rid of it.
delete(hRectangle); %clear your plot
end
I can not test your code at the moment. But try the delete() after your pause()
Hope I could help
  댓글 수: 5
Iñigo de la Joya
Iñigo de la Joya 2019년 12월 18일
Sorry but this doesnt work when I run it on Matlab. It draws a very strange random pattern if you try it.
One question: when I use a function to for example draw a figure, how can I store it inside a variable so that I can then delete the figure?
Andreas Bernatzky
Andreas Bernatzky 2019년 12월 18일
편집: Andreas Bernatzky 2019년 12월 18일
The strange pattern is normal because I use the rand() function.chaos.PNG
you should see some blue and white shapes. (look at picture)
Maybe my example was a little bit confusing.
For your question:
FigureStorage = plot(someXvalues,someYvalues);
delete(FigureStorage);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by