• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
华东 博客
17年国科大博士毕业,曾就职于三星电子,清华博后,目前在某大模型创业公司工作,研究方向大模型、智能体 新浪博客: http://blog.sina.com.cn/u/2463286753
博客园    首页    新随笔    联系   管理    订阅  订阅
MATLAB 随机生成互不重叠的多个矩形

建立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
        


    

  

 

posted on 2015-06-26 18:29  华东博客  阅读(2537)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3