matlab - 通过位置更新持续跟踪球的中心

标签 matlab animation display

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

相关文章:

jquery - UI 的可排序列表的动画过渡

angularjs - 在表格 Angular 中正确显示 JSON

javascript - 通过Javascript在另一个html页面中显示HTML表单

rstudio 代码折叠就像在 matlab GUI 中一样

regex - 仅使用一些参数在 Matlab 中创建字符串

java - 在 Java 中使用 Swing 图标、编号的 .jpg 图像和 for 循环进行动画

javascript - 是否可以使用 React 在 img src 更改上应用 Spring 动画?

size - Kivy 在不同设备上的应用

image - 寻找图像比较/模式识别库

matlab - 使用 matlab 使用概率随机生成器