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 CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by