建立m文件draw_rectangle.m.
其中p生成矩形的个数
function draw_rectangle(p)
t = 1;
x = rand(1)*10;
y = rand(1)*10;
w = rand(1)*10;
h = rand(1)*10;
b =[x y w h];
rectangle(‘Position‘, b)
a{t} =b;
flag =1;
while flag ==1
x = rand(1)*10;
y = rand(1)*10;
w = rand(1)*10;
h = rand(1)*10;
b =[x y w h];
num = 0;
for j = 1:t
if ~rectint(b,a{j})
num = num +1;
end
end
if num == t
hold on;
rectangle(‘Position‘, b)
t = t+1;
a{t} =b;
end
if t == p
break
end
end
% t = 1;
% flag =1
%
% while flag ==1
% for i =1:p
% x = rand(1)*10;
% y = rand(1)*10;
% w = rand(1)*10;
% h = rand(1)*10;
% b =[x y w h];
% % rectangle(‘Position‘, b)
% a{i} =b;
% end
% num = 0;
% for i = 1:(p-1)
% for j =(i+1) :p
% if rectint(a{i},a{j})
% break
% else
% num = num +1;
% end
% end
% end
%
% if num == (p-1)*p/2
% for i = 1:p
% rectangle(‘Position‘, a{i})
% end
% flag = 0;
% end
% end
原文:http://www.cnblogs.com/huadongw/p/4603030.html