7

我正在尝试使用 matlab 编写代码,该代码以我的猫喜欢在屏幕上追逐它的方式模拟激光笔。这是我到目前为止所做的:

figure('menubar','none','color','k')
h = plot(0,'r.','MarkerSize',20);
xlim([-1 1]);  ylim([-1 1])
axis off
phi1=(1+sqrt(5))/2;
phi2=sqrt(3);
step= 0.0001; % change according to machine speed
for t=0:step:100
    set(h,'xdata',sin(t+phi1*t),'ydata',cos(phi2*t))
    drawnow
end

此代码的“问题”如下:

  1. 指针或多或少地以恒定速度移动,并且不会减速到接近停止然后意外地继续。

  2. 轨迹在某种程度上是重复的,虽然我试图用无理数来制作它,但整体运动从右到左是连续的。我认为更剧烈的轨迹变化会有所帮助。

我知道这不是一个传统的编程问题,但我仍然想解决一个编程问题。感谢您的帮助,当然也愿意接受不使用我添加的代码的新方法来回答我的问题。

4

1 回答 1

3

绝妙的问题,太好了,我想我会花 15 分钟的时间自己去试一试。在 YouTube 对激光技术进行广泛研究后,我认为使用运动方程在随机点之间移动会很有效:

n = 20; %number of steps
pos = [0,0]; % initial position
vel = 4; % laser velocity
acc = 400; % laser acelertation
dt = 0.01; % timestep interval
figure
set(gcf,'Position',get(0,'Screensize'));
for i=1:n
    point = rand(1,2);
    dist = 1;
    while dist > 0.05 % loop until we reach the point
        plot(pos(1),pos(2),'o','color','r','MarkerFaceColor','r')
        axis equal
        xlim([0,1])
        ylim([0,1])
        drawnow
        % create random point to move towards
        dist = pdist([point;pos],'euclidean');
        % calculate the direction & mag vector to the point
        dir = (point-pos)/norm((point-pos));
        mag = norm(point-pos);
        % update position
        displ = vel*dt - 0.5*acc*mag*dt^2;
        pos = pos + dir*displ;
    end
end

玩弄参数,直到找到猫喜欢的东西:0)

于 2013-07-20T19:26:04.977 回答