通过位置更新保持跟踪球的中心

Keep tracking the center of a ball with positional updates

在下面的代码中,我试图将球从 90 度移动到 44 度,然后再回到 90 度。我希望能够追踪球中心点的位置。我试图通过绘制一个跟随球的标记来做到这一点。我还希望随着球的移动不断更新球的 (x,y) 位置。

你能帮我修改我的代码以便: 1. 标记保持在球的中心,并且 2. 移动时一直显示标记(球的中心)的(x,y)位置?

这是我到目前为止所做的:

close all; clc; clear;

% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.04; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;

% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
point = plot(0.075,1, 'bo', 'markers', 3);
hold on
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
axis([-1.5 3 -1 3]); grid on;

n = 1;
T=0.15; %Delay between images
for theta = pi/2:-pi/90:0,
    if theta >= pi/4;
        theta = theta;
    else
        theta = pi/2 - theta;
    end
    Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
    xyRot = Arot * [x; y]; % rotates the points by theta
    xyTrans = xyRot;

     point = plot(xyTrans(1, n),xyTrans(2, n), 'bo', 'markers', 3);
     hold on;

    set(myShape2,'Xdata',(xyTrans(1, :)),'Ydata',(xyTrans(2, :)));

    pause(T); %Wait T seconds
end

感谢您的宝贵时间和帮助!

close all; clc; clear;

% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.04; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;

% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
hold on
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
point = plot(0.075,1, 'bo', 'markers', 1);
dim = [.2 .5 .3 .3];
axis([-1.5 3 -1 3]); grid on;
T=0.15;

% Create annotation object to display object center
ano = annotation('textbox',dim,'String',['center position: (','0.075, 1'],'FitBoxToText','on');

for theta = pi/2:-pi/90:0,
    if theta >= pi/4;
        theta = theta;
    else
        theta = pi/2 - theta;
    end
    Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
    xyTrans = Arot * [x; y];

    % Remove plot and annotation object from previous frame
    delete(point)
    delete(ano)

    set(myShape2,'Xdata',(xyTrans(1, :)),'Ydata',(xyTrans(2, :)));

    % Calculate the center of the patch as mean x and y position
    x_pos = mean(xyTrans(1, :));
    y_pos = mean(xyTrans(2, :));
    point = plot(x_pos, y_pos, 'bo', 'markers', 1);        

    ano = annotation('textbox',dim,'String',['center position: (',sprintf('%0.3f', x_pos),', ',...
        sprintf('%0.3f', y_pos),')'],'FitBoxToText','on');

    axis([-1.5 3 -1 3]); grid on;
    pause(T);
end