与Matlab编码有关的两个问题

时间:2018-11-16 00:05:56

标签: matlab matlab-figure

  1. 如何在Matlab中编写代码以计算有多少条风暴线通过线路,有多少条未通过风道,或者有多少条彼此重叠(风暴线和路线)?
  2. 如何在Matlab中编写代码以计算风暴轨迹和重叠线之间的角度? 我已经尝试过了,但是没有用。

    N = 50; % Number of events  
    X = 50; Y = 50; 
    
    P1x = rand(N,1) * X;
    P1y = rand(N,1) * Y ; % initial point
    
    L = lognrnd(2,0.7,[N,1]); % Sample track length 
    Theta = mod(normrnd(90,15,[N,1]),360);
    
    dx = L.*cos(deg2rad(Theta - 90)); 
    dy = L.*sin(deg2rad(Theta - 270));
    
    P2x = P1x + dx; P2y = P1y + dy; % Final point
    
    plot([0 X X 0 0],[0 0 Y Y 0]); hold on
    
    for j = 1:N
        plot(P1x(j),P1y(j),'ro')
        plot([P1x(j) P2x(j)],[P1y(j) P2y(j)],'-') 
    end
    
    k=line([(X+Y)/2.25, (X+Y)/3, (X+Y)/3, (X+Y)/4] , [0, (X+Y)/10, (X+Y)/2.5, (X+Y)/2]);
    
    xlabel('X [km]'); ylabel('Y [km]');
    xlim([-X/4 1.25*X]); ylim([-Y/4 1.25*Y])
    

plot screenshot

2 个答案:

答案 0 :(得分:1)

您可以使用函数 InterX 获取蓝线和风暴线之间的交点。检查以下代码。我在风暴线的情节上几乎没有做任何更改,您不必在那里使用循环。一旦有了交点,就可以使用点积坡度公式获得所需的角度。

N = 50; % Number of events
X = 50; Y = 50;

P1x = rand(N,1) * X;
P1y = rand(N,1) * Y ; % initial point

L = lognrnd(2,0.7,[N,1]); % Sample track length
Theta = mod(normrnd(90,15,[N,1]),360);

dx = L.*cos(deg2rad(Theta - 90));
dy = L.*sin(deg2rad(Theta - 270));

P2x = P1x + dx; P2y = P1y + dy; % Final point

plot([0 X X 0 0],[0 0 Y Y 0]); hold on
% GEt the line 
lx = [(X+Y)/2.25, (X+Y)/3, (X+Y)/3, (X+Y)/4] ;
ly = [0, (X+Y)/10, (X+Y)/2.5, (X+Y)/2];
plot(lx,ly,'b')

plot(P1x,P1y,'ro')
plot([P1x P2x]',[P1y P2y]','-')

% Get intersections 
iwant = zeros(N,1) ;  % this gives whether point intersects or not 
for i = 1:N
    L1 = [lx ; ly] ;
    L2 = [P1x(i) P2x(i) ;P1y(i) P2y(i)] ;
    P = InterX(L1,L2) ;
    if ~isempty(P)
        plot(P(1),P(2),'*r')
        iwant(i) = 1 ;
    end
end

xlabel('X [km]'); ylabel('Y [km]');
xlim([-X/4 1.25*X]); ylim([-Y/4 1.25*Y])

检查从以上代码生成的图形/结果。红色标记的点是您的交点。

enter image description here

答案 1 :(得分:0)

N = 50; % Number of events
X = 50; Y = 50;
P1x = rand(N,1) * X; P1y = rand(N,1) * Y ; % initial point
L = lognrnd(2,0.7,[N,1]); % Sample track length
Theta = mod(normrnd(90,15,[N,1]),360); % Storm track bearing direction
dx = L.*cos(deg2rad(Theta - 90)); dy = L.*sin(deg2rad(Theta - 270));
P2x = P1x + dx; P2y = P1y + dy; % Final point
plot([0 X X 0 0],[0 0 Y Y 0]); hold on
plot(P1x,P1y,'ro');
plot([P1x P2x]',[P1y P2y]','-')
xlabel('X [km]'); ylabel('Y [km]');
xlim([-X/4 1.25*X]); ylim([-Y/4 1.25*Y])
%% Draw rectangles
Vx = [P1x P2x]' ;
Vy = [P1y P2y]' ;
t = 1./2 ;
% Loop to get normals
for i = 1:size(Vx,2)
    N=LineNormals2D([Vx(:,i) Vy(:,i)]') ;

    C = [[Vx(:,i) Vy(:,i)]+t*N ;
        [Vx(:,i) Vy(:,i)]-t*N] ;
    idx = boundary(C(:,1),C(:,2)) ;
    plot(C(idx,1),C(idx,2),'b')
end

通过链接https://in.mathworks.com/matlabcentral/fileexchange/32696-2d-line-curvature-and-normals

向下显示LineNormals2D函数。