Animating the movement of a figure
Afficher commentaires plus anciens
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
Réponses (1)
Andreas Bernatzky
le 13 Déc 2019
Modifié(e) : Andreas Bernatzky
le 13 Déc 2019
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 commentaires
Iñigo de la Joya
le 14 Déc 2019
Andreas Bernatzky
le 15 Déc 2019
Ok and if you include an if-clause for the delete-function?
if(currentIteration < maxIterations)
delete(shape); % if you iterate
end
Or you draw the last known position again after the last iteration? (After the for-loop)
Andreas Bernatzky
le 15 Déc 2019
Thats my solution:
%% shapeRotor
% rotates shape around a rotation point
%% Andreas Bernatzky 15.12.2019
clear all;
close all;
figure(1)
shape = rand(2,100);
ShapeObj = fill(shape(1,:),shape(2,:),'b');
%% now draw your stuff
for(n=1:1:360)
ShapeObj = rotateAround(1,0.5,0.5,ShapeObj.XData,ShapeObj.YData); % I will rotate around (0.5,0.5) dont know if that fits your usecase but does not matter
ShapeObj = fill(ShapeObj.X,ShapeObj.Y,'b');
xlim([0 1])
ylim([0 1]);
pause(0.05)
end
%-----------------------------------------------------------------------------
%% FUNCTIONS
%rotate your shape
% angle in degree
% centerX rotation X
% centerY rotation Y
function newShape = rotateAround(angle,centerX,centerY,xData,yData)
angle = angle * pi/180;
rotatedX = cos(angle) * (xData - centerX) - sin(angle) * (yData-centerY) + centerX;
rotatedY = sin(angle) * (xData - centerX) + cos(angle) * (yData -centerY) + centerY;
newShape.X = rotatedX;
newShape.Y = rotatedY;
end
Iñigo de la Joya
le 18 Déc 2019
Andreas Bernatzky
le 18 Déc 2019
Modifié(e) : Andreas Bernatzky
le 18 Déc 2019
The strange pattern is normal because I use the rand() function.
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);
Catégories
En savoir plus sur Polar Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!