2013-04-14 45 views
0

我正在创建随机的起点和终点。我想绘制与放置在原点的矩形相交/相交的那些人。我发现我的代码错过了一些行,如图所示。在那之后,我想要数一下轨道撞上矩形。对于expample轨道从正面来了,从右侧等如何选择在MatLab中碰到矩形的随机线

退出

我的代码是

function [hot, cold, h] = MuonTracks(tracks)%#eml 

    % NOTE: no variables larger than 1x1 are initialized 

    width = 1e-4; 
    height = 2e-4; 

    % constant used for Laplacian noise distribution 
    bL = 15/sqrt(2); 

    % Loop through all tracks 
    X = []; 
    bhit= []; 
    hot = 0; 
    ii = 0; 
    TopBottom= 0; 
    LeftRight= 0; 
    TopRight= 0; 
    TopLeft= 0; 
    LeftBottom= 0; 
    RightBottom= 0; 
    ihit= 0; 
    while ii <= tracks 

     ii = ii + 1; 

     % Note that I've inlined (== copy-pasted) the original laprnd() 
     % function call. This was necessary to work around limitations 
     % in loops in Matlab, and prevent the nececessity of those HUGE 
     % variables. 
     % 
     % Of course, you can still easily generalize all of this: 

     % the new data 
     u = rand-0.5; 

     Ystart = 15; 
     Xstart = 80*rand-40; 
     Xend = Xstart - bL*sign(u)*log(1-2*abs(u)); 
     %Xend=laprnd(tracks,1,Xstart,15); 

     b = (Ystart*Xend)/(Xend-Xstart); 


     % the test 
     if ((b < height && b > 0)) ||... 
      (Xend < width/2 && Xend > -width/2) 

      hot = hot+1; 

      % growing an array is perfectly fine when the chances of it 
      % happening are so slim 
      X = [X [Xstart; Xend]]; %#ok 
      bhit=b; 

     end 
    end 

    % This is trivial to do here, and prevents an 'else' 
    cold = tracks - hot; 

    % Now plot the chosen ones 
    h = figure; 
    hold all  
    %Y = bsxfun(@times, 15, ones(size(X))); 
    if (size(X)==0) 
     %Disp('No hits were found'); 
     msgbox('No tracks were found','Result','warn'); 
    elseif (size(X)>1) 
     Y = bsxfun(@times, [15; 0], ones(size(X))); 
     plot(X, Y, 'r'); 
     msgbox([num2str(hot) ' tracks were found'],'Result','help',num2str(hot)); 
    else 
     Y = bsxfun(@times, [15; 0], ones(size(X))); 
     plot(X, Y, 'r'); 
     msgbox([num2str(hot) ' track was found'],'Result','help',num2str(hot)); 
    end 
    %X; 
    %Y; 
    %size(X,2) 
    while ihit<size(X,2) 

     ihit=ihit+1 
     %X(2,ihit) 

     if ((X(2,ihit)==-width && (bhit<=0 && bhit<=height))&&(bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width))) 
      LeftBottom=LeftBottom+1; 
     elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height))) 
      TopRight=TopRight+1; 
     elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width))) 
      TopBottom=TopBottom+1; 
     elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height))) 
      LeftRight=LeftRight+1; 
     elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width))) 
      TopLeft=TopLeft+1; 
     elseif ((X(2,ihit)==width && (bhit<=0 && bhit<=height)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width))) 
      RightBottom=RightBottom+1; 
     else 
      display('sth is wrong'); 
     end 
     X(2,ihit) 
    end 
    %X(1,:); 
    %Y(1,:); 
    LeftBottom 
    TopRight 
    TopBottom 
    LeftRight 
    TopLeft 
    RightBottom 
    %display('sdfghjk'); 
end 

任何想法会更受欢迎!

+0

那么你想随机生成一条与你的矩形相交的线,或者只是从给定的一组线中选择一条呢? –

+0

@EitanT:非常感谢您的评论!我已经产生了起点和终点。所以我想要的是绘制与rectange相交的线,并计算每个交点在哪一侧出现的次数(即:总共10个交点,从上到下2个,从左到右3个,从上到下4个左边,右边1,右边0,左边0,右上边0)。 – Thanos

回答

0

Here你有一个功能,能够相交整个组的段并返回交点。我希望它有帮助。