简体   繁体   中英

how to move circle using plot in Matlab

I'd like to draw a circle and move it in Matlab plot figure. I'm using

 viscircles([8.1, 8.5], 1);

to draw circles. How do I call this again to draw a new circle and delete the original circle? Also is there a way I can use

drawnow

function to do this?

Instead of removing and redrawing, just move the circle by introducing some constant in the X and Y data.

%%%%Borrowing some code from irreducible's answer%%%%
xc=1; yc=2; r=3;
th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
h = plot(x, y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-20 20 -20 20]); %Fixing axis limits 
for k=1:20       %A loop to visualise new shifted circles
    h.XData = x + randi([-10 10],1);   %Adding a constant in the x data
    h.YData = y + randi([-10 10],1);   %Adding a constant in the y data
    pause(0.5);  %Pausing for some time just for convenient visualisation
end

One possibility is to create your own circle function which returns the plot handle:

function h = my_circle(xc,yc,r)
% xc x-center of circle
% yc y-center of circle
% r radius

th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
hold on
h = plot(x, y);
hold off;

Once having this you can plot your circle

h = my_circle(1,2,3);

and delete it if you dont need it anymore:

delete(h)

Afterwards you can plot a new one:

h2 = my_circle(1,2,4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM