Moving a circle around a point along with it.

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일

1 개 추천

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

The code can be done more precise using below code
% 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;
h1 = plot(x, y, 'ro');
h2 = viscircles([x, y], r);
pause(0.1) % choose a suitable value as per your need
h1.XData = 0;
h1.YData = 0;
h1.Color = 'w';
delete(h2)
% Define new position of point by interactive input
new_x = randi([-10 10],1) %input('Enter new x coordinate:')
new_x = 3
new_y = randi([-10 10],1) %input('Enter new y coordinate:')
new_y = -1
% Move point and circle to new position
viscircles([new_x, new_y], r,'Color','b');
Lim = abs(max([new_x new_y]));
xlim([-Lim-2 Lim+2]);ylim([-Lim-2 Lim+2]);
Thanks @Sugandhi and @VBBV for the replies. It was helpful. Moreover, can you guys send me the code showing an animation for the same.
Like a point which is at 5,5 which has been encircled by a circle havinf center at 5,5 and radius 1. As the point moves from (5,5) to (0,0), the circle should around it also should move with it. Is this possible. Point can move straight or in any random way, but straight path would be easy to implement i guess.
clc
clear all
close all
x_r(1,1) = 5;
y_r(1,1) = 5;
dt = 0.01;
t_span = 10;
t = 0:dt:t_span;
r = 2;
Xc = x_r(1,1);
Yc = y_r(1,1);
for n = 1 : length(t)
xd(1,:) = r*cos(t) + Xc;
yd(1,:) = r*sin(t) + Yc;
x_r(1, n+1) = x_r(1, n) + dt * (0.4);
y_r(1, n+1) = y_r(1, n) + dt * (0.4);
end
figure(1)
axis([2 15 0 20]);
hold on
p = plot( x_r(1,1), y_r(1,1),'o','MarkerFaceColor','red','MarkerSize',3.5);
hold on
plot(xd,yd, 'o','MarkerFaceColor','blue','MarkerSize',1);
hold on
for k=1:length(t)
title(['Time = ',num2str(k*dt)])
hold on
p.XData = x_r(1,k,1);
p.YData = y_r(1,k,1);
desired.XData = xd(1,k,1);
desired.YData = yd(1,k,1);
drawnow
pause(0.001);
end
In the above code, the point is moving but the circle is at the original position only. How can I make the circle move along with the point.
Got it.
x_r(1,1) = 5;
y_r(1,1) = 5;
dt = 0.01;
t_span = 10;
t = 0:dt:t_span;
r = 2;
Xc = x_r(1,1);
Yc = y_r(1,1);
figure(1)
axis([2 15 0 20]);
hold on
% draw the circle centered at the initial position of the point
th = linspace(0, 2*pi, 100);
xc = r*cos(th) + Xc;
yc = r*sin(th) + Yc;
circle = plot(xc, yc, 'b-');
% plot the initial position of the point
p = plot( x_r(1,1), y_r(1,1),'ro','MarkerFaceColor','red','MarkerSize',3.5);
for n = 1 : length(t)
x_r(1, n+1) = x_r(1, n) + dt * (0.4);
y_r(1, n+1) = y_r(1, n) + dt * (0.4);
% update the position of the point
p.XData = x_r(1,n+1);
p.YData = y_r(1,n+1);
% update the position of the circle to match the new position of the point
Xc = x_r(1,n+1);
Yc = y_r(1,n+1);
xc = r*cos(th) + Xc;
yc = r*sin(th) + Yc;
circle.XData = xc;
circle.YData = yc;
title(['Time = ',num2str(n*dt)])
drawnow
pause(0.01);
end
This is what I wanted. Thanks again
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
Thanks for this too. This also works.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

질문:

2023년 4월 27일

댓글:

2023년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by