Moving a circle around a point along with it.

54 vues (au cours des 30 derniers jours)
Pallov Anand
Pallov Anand le 27 Avr 2023
Commenté : Pallov Anand le 30 Avr 2023
Suppose i have a point located at (5,5) having a circle of radius 1 around it. If the point moves from 5,5 to any other point say (0,0). The circle should also move with it.
Can someone give the matlab code for this.

Réponse acceptée

Sugandhi
Sugandhi le 28 Avr 2023
Hi Pallov Anand,
I understand that If the point moves from 5,5 to any other point say (0,0). The circle should be drawn with center as (0,0).The code you requested could be like this:
% Define initial position of point and radius of circle
x = 5;
y = 5;
r = 1;
% Create figure window and draw initial point and circle
figure;
hold on;
plot(x, y, 'ro');
viscircles([x, y], r);
% Define new position of point
new_x = 0;
new_y = 0;
% Move point and circle to new position
dx = new_x - x;
dy = new_y - y;
x = new_x;
y = new_y;
clf;
hold on;
plot(x, y, 'ro');
viscircles([x, y], r);
% Repeat the above code to move the point and circle to any other position as desired
In this code, x and y represent the x and y coordinates of the point, and r represents the radius of the circle. The `viscircles` function is used to draw the circle around the point.
To move the point and circle to a new position, we first define the new position by setting new_x and new_y to the desired coordinates. We then calculate the difference between the new position and the current position using dx = new_x - x and dy = new_y - y. Finally, we update x and y to the new position, and redraw the point and circle at the new location.
You can repeat the code to move the point and circle to any other position as desired.
  6 commentaires
VBBV
VBBV le 29 Avr 2023
You can use Matlab built in function animatedline to do the same in simpler way as below
clearvars
close all
% Present position of centre of circle
Xc = 2;
Yc = 4;
r = 3;
% define axes
axis([-10,10,-10,10])
th = linspace(0, 2*pi, 1000);
% new position ( can also get from user input)
Xnew = 5;
Ynew = 3;
DisX = linspace(Xc,Xnew,30);
DisY = linspace(Yc,Ynew,30);
h = animatedline('Marker','.','Color','b');grid
for k = 1:length(DisX)
xc = r*cos(th) + DisX(k);
yc = r*sin(th) + DisY(k);
addpoints(h,xc,yc);
drawnow
pause(0.1)
clearpoints(h)
end
addpoints(h,xc,yc)
drawnow
Pallov Anand
Pallov Anand le 30 Avr 2023
Thanks for this too. This also works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by