Search code examples
matlabmatlab-figure

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?


Solution

  • 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