【发布时间】:2014-11-15 13:29:28
【问题描述】:
我必须模拟蚂蚁在它们的家(黑框)和食物(黄框)之间移动。这些三色盒子是蚂蚁。我为绘制所示图形编写的代码如下:
% background
background()
% making ants
handle = zeros(10,3)
handle = makingAnts(10) ;
% moving ants
movingAnts(hand)
功能背景:
function background()
figure
hold on
axis equal
axis([0 100 0 100])
pos = rand(1,2).*75
rectangle('position',[0 0 10 10],'facecolor','k')
rectangle('position',[pos 25 25],'facecolor','y')
end
功能制作蚂蚁:
function [h] = makingAnts(n)
h = zeros(10,3)
dia = [2 2]
for i = 1:n
pos = rand(1,2).* 95 ;
h(i,1) = rectangle('position',[pos dia],'facecolor',[0.2 0.6 1])
g1 = get(h(i,1),'position')
h(i,2) = rectangle('position',[(g1(1)+2) (g1(2)+2) 2 2],'facecolor',[0.4 1 0.6])
h(i,3) = rectangle('position',[(g1(1)+4) (g1(2)+4) 2 2],'facecolor',[1 0.8 1])
end
end
现在我必须移动蚂蚁。虽然我已经编写了代码,但它不起作用。我需要帮助让蚂蚁移动。
我写的代码:
function movingAnts(h)
% moving 1 ant
pos = get(h(1),'position')
m = pos(1)
n = pos(2)
for i = 1:50
set(h(1),'position',[(m+0.2) (n+0.2) 2 2])
pause(0.05)
end
end
【问题讨论】:
-
有人能解释一下为什么这个问题得到了这么多的支持吗?
-
@franz1 - 很可能是因为这是一个很酷的问题要解决,尽管问题陈述不像本文中的答案那么冗长。