Moving a circle around a point along with it.

7 ビュー (過去 30 日間)
Pallov Anand
Pallov Anand 2023 年 4 月 27 日
コメント済み: Pallov Anand 2023 年 4 月 30 日
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.

採用された回答

Sugandhi
Sugandhi 2023 年 4 月 28 日
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 件のコメント
VBBV
VBBV 2023 年 4 月 29 日
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 2023 年 4 月 30 日
Thanks for this too. This also works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by