www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/cgsetblockpos.m

    function newpos = cgsetblockpos(blk,pos,AllBlockPositions)
% CGSETBLOCKPOS
% newpos = cgsetblockpos(BlkIdentifier,DesiredPosition,AllBlockPositions);

%  Copyright 2000-2012 The MathWorks, Inc. and Ford Global Technologies, Inc.


space = 20;
if nargin ~= 3
   error(message('mbc:cgsetblockpos:TooFewInputs'))
else
   % Make sure blocks don't go too high
   if pos(2)<40
      delta = 40 - pos(2);
      pos = pos + [0 delta 0 delta];
   end
   right = ~(pos(1)>AllBlockPositions(:,3));
   left = ~(pos(3)<AllBlockPositions(:,1));
   bottom = ~(pos(2)>AllBlockPositions(:,4));
   top = ~(pos(4)<AllBlockPositions(:,2));
   overlap = (left&right)&(top&bottom);
   if any(overlap)      
      % Find first overlapping block
      index = find(overlap);
      index=index(1);
      if pos(2)>AllBlockPositions(index,2) || AllBlockPositions(index,2)<(pos(4)-pos(2))
         % move it down
         delta = AllBlockPositions(index,4)-pos(2);
      else
         % move it up
         delta = pos(4)-AllBlockPositions(index,2);
      end
      %shift
      newpos = pos + [0 delta+space 0 delta+space];
      overlap = ~((newpos(1)>AllBlockPositions(:,3))|(newpos(3)<AllBlockPositions(:,1)))&~((newpos(2)>AllBlockPositions(:,4))|(newpos(4)<AllBlockPositions(:,2)));
      if any(overlap)
         index = find(overlap);
         index=index(1);
         if pos(1)>AllBlockPositions(index,1)
            % move it left
            delta = AllBlockPositions(index,3)-pos(1);
         else
            % move it right
            delta = pos(3)-AllBlockPositions(index,1);
         end
         %shift 
         newpos = pos + [0 delta+space 0 delta+space];
      end
   else
      newpos = pos;
   end
   set_param(blk,'position',newpos);
end