在下面的代码中,我试图将球从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

关于matlab - 通过位置更新不断跟踪球的中心,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33946731/

10-09 02:47