我正试图用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
此代码的"问题"如下:
指针或多或少以恒定的速度移动,不会减速到接近停止,然后意外地继续。
轨迹有点重复,尽管我试图使用无理数,但总体运动从右到左是连续的。我认为更剧烈的轨迹变化会有所帮助。
我知道这不是一个传统的编程问题,但我仍然想解决编程问题。我很感谢你的帮助,当然,我也愿意用新的方法来回答我的问题,而不使用我添加的代码。
这个问题太好了,我想我会用我生命中的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)