⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lineext.m

📁 数字通信第四版原书的例程
💻 M
📖 第 1 页 / 共 2 页
字号:
                 layout(n_be+1,1) = layout(n_be,1);
              elseif n_be+1 ~= n_l
                 layout(n_be,1)   = x_max+10+abs(layout(n_be,2)-layout(n_be+1,2))/40;
                 layout(n_be+1,1) = layout(n_be,1);
              else
                 up = 0; % there is no way up
              end;
           else
              up =0;  %there is no way to go up
           end;
        end;
      end;
      while dn
        % find the cross
        [x_min,x_max,y_min,y_max,test] =...
		 linemima(layout(n_be:n_be+1, :), blklocs);
        if isempty(test)
           dn = 0;
           up = 0;
        else
           %move down to position (3)
           if isempty(blkxchk([layout(n_be-1,:);x_min-10, layout(n_be,2)], blklocs))
              if x_max < layout(n_be+1,1)
                 layout(n_be,1)   = x_min - 10 - abs(layout(n_be,2)-layout(n_be+1,2))/40;
                 layout(n_be+1,1) = layout(n_be,1);
              elseif n_be+1 ~= n_l
                 layout(n_be,1)   = x_min - 10 - abs(layout(n_be,2)-layout(n_be+1,2))/40;
                 layout(n_be+1,1) = layout(n_be,1);
              else
                 dn = 0; % there is no way dn
              end;
           else
              dn =0;  %there is no way to go dn
              flag(n_be) = 1 + flag(n_be);
           end;
        end;
      end;

      %switch back to the direction
      if (direct == 2) | (direct == 1)
         layout  = -layout;
          blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
          blklocs(:,[2,4]) = fliplr(-blklocs(:,[2,4]));
      end;

      if (direct == 1) | (direct == 3)
         %swirch x and y
         layout(:,1) = - layout(:,1);
         layout = fliplr(layout);
         blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
         blklocs(:,1:2) = fliplr(blklocs(:,1:2));
         blklocs(:,3:4) = fliplr(blklocs(:,3:4));
      end;
%      if (direct == 1) | (direct == 3)
%%        %swirch x and y
%        layout = fliplr(layout);
%        blklocs(:,1:2) = fliplr(blklocs(:,1:2));
%        blklocs(:,3:4) = fliplr(blklocs(:,3:4));
%      end;
%      if (direct == 2) | (direct == 3)
%        %reverse direction
%        layout(:,1) = 2000 - layout(:,1);
%        blklocs(:,[1 3]) = 2000 - blklocs(:,[1 3]);
%        blklocs(:,[1 3]) = fliplr(blklocs(:,[1 3]));
%      end;
   elseif flag(n_be) < 10
      %                                _______|
      % in the case of brake to ______|         seg1, seg2, seg3, seg4
      % all of the cases, brake layout(n_be:  n_be+1, :)
      % and                     layout(n_be+1:n_be+2, :)
      % except for the case of n_be+1 == n_en when the 
      % connection lines will be go a reverse way to match the requirement

      %in case of need to reverse
      revers = 0;
      if n_be+1 == n_l
        revers = 1;
        layout = flipud(layout);
        n_be = 1; n_en = 2;
      end;

      %make all of the begining line to be left-->right direction
      direct = linedir(layout(n_be:n_be+1,:));
      if direct < 0
        %I am not taking care of randon direction now
        return;
      end;

      if (direct==1) | (direct==3)
         %switch x and y
         layout = fliplr(layout);
         layout(:,1) = -layout(:,1);
         blklocs(:,1:2) = fliplr(blklocs(:,1:2));
         blklocs(:,3:4) = fliplr(blklocs(:,3:4));
         blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
      end;

      if (direct==2) | (direct==1)
          %reverse direction
          layout = - layout;
          blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
          blklocs(:,[2,4]) = fliplr(-blklocs(:,[2,4]));
      end;

%      if (direct == 2) | (direct == 3)
%        %reverse direction
%        layout(:,1) = 2000 - layout(:,1);
%        blklocs(:,[1 3]) = 2000 - blklocs(:,[1 3]);
%        blklocs(:,[1 3]) = fliplr(blklocs(:,[1 3]));
%      end;
%      if (direct == 1) | (direct == 3)
%        %swirch x and y
%        layout = fliplr(layout);
%        blklocs(:,1:2) = fliplr(blklocs(:,1:2));
%        blklocs(:,3:4) = fliplr(blklocs(:,3:4));
%      end;
      %now everyone has direction 0      

      %calculate the minimum and maximum
      [x_min,x_max,y_min,y_max] = linemima(layout(n_be:n_be+1,:),blklocs);

      %direction of the second line
      direct2 = linedir(layout(n_be+1:n_be+2,:));
      %by our setup, direct2 should be either 1 or 3

      %make space for the new part, enlarge two points
      layout(n_be+3:n_l+2,:) = layout(n_be+1:n_l,:);
      flag(n_be+3:n_l+2) = flag(n_be+1:n_l);
      flag(n_be+1:n_be+2) = flag(n_be+1:n_be+2)*0;
      n_l = n_l + 2;

      %braking n_be:n_be+1 line
      layout(n_be + 1, 1) = min((layout(n_be, 1) + x_min)/2, x_min-10);
      layout(n_be + 2, 1) = layout(n_be, 1);
      if direct2 == 1
        layout(n_be + 3, 2) = max((layout(n_be+3, 2) + y_max)/2, y_max+10);
      else
        layout(n_be + 3, 2) = min((layout(n_be+3, 2) + y_min)/2, y_min-10);
      end;
      layout(n_be + 2, 1) = layout(n_be + 1, 1);
      layout(n_be + 2, 2) = layout(n_be + 3, 2);

      odir = 0;  %flag for failed 
      addir = 0; %first section is fixed
      sudir = 0; %second section is fixed
      thdir = 0; %third section is fixed
      frdir = 0; %fourth section is fixed
      again = 1; test = 0;
      while again & (test < 10)
        test = test + 1;
        if addir == 0 %seg1
          %Need to check up or fix
          [tmp_x_min,tmp_x_max,tmp_y_min,tmp_y_max,tmp_x] = ...
		linemima(layout(n_be:n_be+1,:),blklocs);
          addir = 1;
          if ~isempty(tmp_x)
            if tmp_x_max < x_min                    %not cross the old limit
              layout(n_be+1,1) = (x_min + tmp_x_max)/2;
              sudir = 0; 			    %seg2 relocated
            elseif tmp_x_min > layout(n_be,1)       %not change the seg1 direction.
              layout(n_be+1,1) = min((layout(n_be,1) + tmp_x_min)/2, tmp_x_min - 10);
              thdir = 0; sudir = 0;                 %seg2 relocated; seg3 extended
            else
              %need to reduce the length of seg2
              odir = 1; addir = 0; sudir = 0; again = 0;
            end;
            layout(n_be+2,1) = layout(n_be+1,1);
          end;
        end;

        if sudir == 0 %seg2
          %Need to checkup or fix
          [tmp_x_min,tmp_x_max,tmp_y_min,tmp_y_max,tmp_x] = ...
		linemima(layout(n_be+1:n_be+2,:),blklocs);
          sudir = 1;
          if ~isempty(tmp_x)
            if (tmp_x_max < x_min)			% still have space for pass
              layout(n_be + 2, 1) = (tmp_x_max + x_min)/2;
            elseif (tmp_y_min > y_max) & (direct2 == 1) % still have space for pass
              layout(n_be + 2, 2) = (tmp_y_min + y_max)/2;
              thdir = 0;				% seg3 moved
              frdir = 0;				% seg4 extended
            elseif (tmp_y_max < y_min) & (direct2 == 3) % still have space for pass
              layout(n_be + 2, 2) = (tmp_y_max + y_min)/2;
              thdir = 0;				% seg3 moved
              frdir = 0;				% seg4 extended
            elseif (tmp_x_min > layout(n_be, 1))        % horizontal move
              layout(n_be + 2, 1) = min((tmp_x_min + layout(n_be, 1))/2, tmp_x_min - 10);
              sudir = 0;				% seg2 moved
              thdir = 0;				% seg3 moved
            else
              %there is no way to move
              sudir = 0;
              odir = 1;
              again = 0;
            end;
          end;
          %adjust the points
          layout(n_be+3,2) = layout(n_be+2,2);
          layout(n_be+1,1) = layout(n_be+2,1);
        end;
        if thdir == 0
          %Need to checkup or fix
          [tmp_x_min,tmp_x_max,tmp_y_min,tmp_y_max,tmp_x] = ...
		linemima(layout(n_be+2:n_be+3,:),blklocs);
          thdir = 1;
          if ~isempty(tmp_x)
            if (tmp_x_max < x_min)
              layout(n_be + 2,1) = (tmp_x_max + x_min)/2;
              sudir = 0;
            elseif (tmp_y_max > y_max) & (tmp_y_max < layout(n_be+4,2)) & (direct2 == 1)
							% still have space for pass
              layout(n_be + 3, 2) = max((tmp_x_max + layout(n_be+4,2))/2, tmp_x_max);
              sudir = 0; 
	      thdir = 0;
            elseif (tmp_y_min < y_min) & (tmp_y_min > layout(n_be+4,2)) & (direct2 == 3)
							% still have space for pass
              layout(n_be + 3, 2) = min((tmp_x_min+layout(n_be+4,2))/2, tmp_x_min-10);
              sudir = 0; 
	      thdir = 0;
            elseif (tmp_y_min > layout(n_be+4, 2)) & (direct2 == 3)
              layout(n_be + 3, 2) = (tmp_y_min > layout(n_be+4, 2))/2;
            elseif (tmp_y_max < layout(n_be+4, 2)) & (direct2 == 13)
              layout(n_be + 3, 2) = (tmp_y_max > layout(n_be+4, 2))/2;
            else
              %there is no way to move
              sudir = 0;
              odir = 1;
              again = 0;
            end;
            layout(n_be + 2, 2) = layout(n_be + 3, 2);
            layout(n_be + 1, 1) = layout(n_be + 2, 1);
          end;
        end;
        if frdir == 0;
          %
          [tmp_x_min,tmp_x_max,tmp_y_min,tmp_y_max,tmp_x] = ...
		linemima(layout(n_be+3:n_be+4,:),blklocs);
          thdir = 1;
          if ~isempty(tmp_x)
            if (tmp_y_max < layout(n_be+4,2)) & (direct2 == 1)
              layout(n_be + 3, 2) = max((layout(n_be+4, 2) + tmp_y_max)/2, tmp_y_max + 10);
              thdir = 0;
            elseif (tmp_y_min > layout(n_be+4, 2)) & (direct2 == 3)
	      layout(n_be + 3, 2) = min((layout(n_be+4, 2) + tmp_y_min)/2, tmp_y_min - 10);
              thdir = 0;
            else
              thdir = 0; odir = 0; %wait for next around
              again = 0;
            end;
            layout(n_be + 2, 2) = layout(n_be + 3, 2);
          end;
        end;
      end;

      %back to the original direction
      if (direct == 2) | (direct == 1)
         layout  = -layout;
          blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
          blklocs(:,[2,4]) = fliplr(-blklocs(:,[2,4]));
      end;

      if (direct == 1) | (direct == 3)
         %swirch x and y
         layout(:,1) = - layout(:,1);
         layout = fliplr(layout);
         blklocs(:,[1,3]) = fliplr(-blklocs(:,[1,3]));
         blklocs(:,1:2) = fliplr(blklocs(:,1:2));
         blklocs(:,3:4) = fliplr(blklocs(:,3:4));
      end;

%      if (direct == 1) | (direct == 3)
%        %swirch x and y
%        layout = fliplr(layout);
%        blklocs(:,1:2) = fliplr(blklocs(:,1:2));
%        blklocs(:,3:4) = fliplr(blklocs(:,3:4));
%      end;
%      if (direct == 2) | (direct == 3)
%        %reverse direction
%        layout(:,1)  = 2000 - layout(:,1);
%        blklocs(:,[1 3]) = 2000 - blklocs(:,[1 3]);
%        blklocs(:,[1 3]) = fliplr(blklocs(:,[1 3]));
%      end;

      if revers
        layout = flipud(layout);
        revers = 0;
      end;
   else
      n_l = 11; % I cannot do anything better
   end; %if straight

   %test if there is anything to be done
   [x_min,x_max,y_min,y_max,to_do,n_be,n_en]=...
	linemima(layout,blklocs);
end;  %while
%check if we could reduce some redundent segment

for j=1:3
  [n_l, m_l] = size(layout);
  if n_l > 5
     tmp = [];
     i = 1;
     while i < n_l - 3
       i = i+1;
       tmp(1,:) = layout(i-1,:);
       tmp(3,:) = layout(i+2,:);
       if rem(i,2) == 0
         tmp(2,2) = layout(i-1,2);
         tmp(2,1) = layout(i+2,1);
       else
         tmp(2,1) = layout(i-1,1);
         tmp(2,2) = layout(i+2,2);
       end;
       if isempty(blkxchk(tmp(1:2,:),blklocs))
          if isempty(blkxchk(tmp(2:3,:),blklocs))
             layout(i,:) = tmp(2,:);
             layout(i+1:i+2,:) = [];
             n_l = n_l - 2;
          end;
       end;
     end;
  end;
end;
%simplify: check if the lines can be simplified be combining some of them

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -