%% 建立GUI,建立plot按钮
clf;
clc;
clear all;
plotbutton=uicontrol('style','pushbutton',...
'string','Run', ...
'fontsize',12, ...
'position',[100,400,50,20], ...
'callback', 'run=1;');
%define the stop button
erasebutton=uicontrol('style','pushbutton',...
'string','Stop', ...
'fontsize',12, ...
'position',[200,400,50,20], ...
'callback','freeze=1;');
%define the Quit button
quitbutton=uicontrol('style','pushbutton',...
'string','Quit', ...
'fontsize',12, ...
'position',[300,400,50,20], ...
'callback','stop=1;close;');
number = uicontrol('style','text', ...
'string','1', ...
'fontsize',12, ...
'position',[20,400,50,20]);
%CA setup
BackGound=zeros(10,100);
%% 数据初始化
axis equal
axis tight %坐标轴最大值最小值一样
%%
CarDeta=0;
deta=0.3;
%%
l=1;
w=1;
wa=1;
pnumber=0;
carnumber=0;
stop=0; %wait for a quit button push
run=0; %wait for a draw
freeze=0; %wait for a freeze(冻结)
%%
a(1:100,1:140)=0;
a(1:40,51:90)=1;
a(61:100,51:90)=1;
a(1:100,1:2)=2;
a(1:2,1:140)=2;
a(99:100,1:140)=2;
a(1:100,139:140)=2;
a(45:55,1:2)=0;
%二楼课桌椅
%a(35:40,100:110)=2;
%a(35:40,120:130)=2;
%a(25:30,100:110)=2;
%a(25:30,120:130)=2;
%a(15:20,100:110)=2;
%a(15:20,120:130)=2;
%a(60:65,100:110)=2;
%a(60:65,120:130)=2;
%a(70:75,100:110)=2;
%a(70:75,120:130)=2;
%a(80:85,100:110)=2;
%a(80:85,120:130)=2;
%二楼讲台
%a(96:99,100:130)=3;
%一楼障碍物布置
%a(5:35,40:45)=4;
%a(65:95,40:45)=4;
%a(10:15,10:15)=4;
%a(10:15,25:30)=4;
%a(30:35,10:15)=4;
%a(30:35,25:30)=4;
%a(85:90,10:15)=4;
%a(85:90,25:30)=4;
%a(65:70,10:15)=4;
%a(65:70,25:30)=4;
%门口障碍物
a(44:46,8:16)=7;
a(54:56,8:16)=7;
c=zeros(size(a));
pp=zeros(size(a));%是人员的移动规则
%生成人的循环矩阵
temp=3000;
%%
while temp>0
[maxy,maxx]=size(a);
x=round((maxx-6)*rand(1))+3;
y=round((maxy-6)*rand(1))+3;
if a(y,x)==0
c(y,x)=rand(1);
temp=temp-1;
end
end
result=a+c;
imshow(result,'InitialMagnification','fit')
t=cputime;
%% 按下run,开始循环。
while (stop==0)
if(run==1)
d=zeros(size(a));
%%
distance1=zeros(100,140);
for x=1:140
for y=1:100
if(c(y,x)~=0)
distance1(y,x)=sqrt((x-2).^2+(y-50).^2);
% distance=sqrt((index_x(n)-2)^2+(index_y(n)-50)^2);
end
end
end
distance_num=unique(distance1((distance1~=0)));
count=length(distance_num);
%%
for n=1:count
%%
% 取得等距离人的所有坐标,有几人就循环几次
[x_per,y_per]=find(distance_num(n)==distance1);
for person=1:length(x_per)
x=x_per(person); y=y_per(person);
TempX=x-1:x+1;%表示3*3人矩阵的坐标
TempY=y-1:y+1;%表示3*3人矩阵的坐标
b=zeros(3,3);
for x1=1:3
for y1=1:3
if a(x1+x-2,y1+y-2)>0%
b(x1,y1)=1000000;%生成一个无穷大的数这样就让人可以规避障碍物
end
if d(x1+x-2,y1+y-2)>0 %&&~(x1==2&&y1==2)
b(x1,y1)=1000000;
end
end
end
for nj=1:3
b(nj,:)=b(nj,:)+(TempY-2).^2;
b(:,nj)=b(:,nj)+(TempX-50)'.^2;
end
% end
% b=sqrt(b);
[tempx,indexX]=min(b);%表示选择3*3矩阵中最小值 选出各行中的最小值
[temp,indexY]=min(tempx);%再筛选出各行中的最小值的基础中 再选出这些最小值里的最小值
AimX=x+indexX(indexY)-2;
AimY=y+indexY-2;
d(AimX,AimY)=1;%选择目标
end
end
c=d;%d矩阵是覆盖矩阵,避免出现大误差,防止矩阵生成上一个人残留
c(1:100,1:2)=0;
result=a+c;
imshow(a+c,'InitialMagnification','fit')
pause(0.05)
%%
l=l+1;
stepnumber = 1 + str2num(get(number,'string'));
set(number,'string',num2str(stepnumber));
%% 边界
end
if (freeze==1)
run = 0;
freeze = 0;
end
drawnow
end
评论0