在Matlab中检测两条WGS84线的交点。

huangapple go评论59阅读模式
英文:

Detecting intersection of two WGS84 lines in Matlab

问题

我想在Matlab中检测两条WSG84线的交点,已知以下信息。

红线:latStart0, lonStart0, latEnd0, lonEnd0

蓝线:latStart1, lonStart1, latEnd1, lonEnd1

英文:

I would like to detect intersection of two WSG84 lines in Matlab given the information below known.

RedLine: latStart0, lonStart0, latEnd0, lonEnd0

BlueLine: latStart1, lonStart1, latEnd1, lonEnd1
在Matlab中检测两条WGS84线的交点。

How can i achieve this ?

答案1

得分: 0

以下是解决方案:

function intersectionPoint = findIntersection(line1_start, line1_end, line2_start, line2_end)
    line1_start_rad = deg2rad(line1_start);
    line1_end_rad = deg2rad(line1_end);
    line2_start_rad = deg2rad(line2_start);
    line2_end_rad = deg2rad(line2_end);
    
    % 计算线段的分母
    denom = (line1_end_rad(1) - line1_start_rad(1)) * (line2_end_rad(2) - line2_start_rad(2)) - ...
        (line1_end_rad(2) - line1_start_rad(2)) * (line2_end_rad(1) - line2_start_rad(1));

    if abs(denom) < eps
        intersectionPoint = struct('latitude', NaN, 'longitude', NaN);
        return;
    end

    numer = (line2_end_rad(1) - line2_start_rad(1)) * (line1_start_rad(2) - line2_start_rad(2)) - ...
        (line2_end_rad(2) - line2_start_rad(2)) * (line1_start_rad(1) - line2_start_rad(1));

    t1 = numer / denom;
    
    if t1 < 0
        intersectionPoint = struct('latitude', NaN, 'longitude', NaN);
        return;
    end
    
    % 计算交点
    lonIntersection_rad = line1_start_rad(2) + (line1_end_rad(2) - line1_start_rad(2)) * t1;
    latIntersection_rad = line1_start_rad(1) + (line1_end_rad(1) - line1_start_rad(1)) * t1;

    latIntersection_deg = rad2deg(latIntersection_rad);
    lonIntersection_deg = rad2deg(lonIntersection_rad);

    intersectionPoint = struct('latitude', latIntersection_deg, 'longitude', lonIntersection_deg);
end
英文:

Here it is the solution:

function intersectionPoint = findIntersection(line1_start, line1_end, line2_start, line2_end)
    line1_start_rad = deg2rad(line1_start);
    line1_end_rad = deg2rad(line1_end);
    line2_start_rad = deg2rad(line2_start);
    line2_end_rad = deg2rad(line2_end);
    
    % Calculate the denominator for the lines
    denom = (line1_end_rad(1) - line1_start_rad(1)) * (line2_end_rad(2) - line2_start_rad(2)) - ...
        (line1_end_rad(2) - line1_start_rad(2)) * (line2_end_rad(1) - line2_start_rad(1));

    if abs(denom) &lt; eps
        intersectionPoint = struct(&#39;latitude&#39;, NaN, &#39;longitude&#39;, NaN);
        return;
    end

    numer = (line2_end_rad(1) - line2_start_rad(1)) * (line1_start_rad(2) - line2_start_rad(2)) - ...
        (line2_end_rad(2) - line2_start_rad(2)) * (line1_start_rad(1) - line2_start_rad(1));

    t1 = numer / denom;
    
    if t1 &lt; 0
        intersectionPoint = struct(&#39;latitude&#39;, NaN, &#39;longitude&#39;, NaN);
        return;
    end
    
    % Calculate the intersection point
    lonIntersection_rad = line1_start_rad(2) + (line1_end_rad(2) - line1_start_rad(2)) * t1;
    latIntersection_rad = line1_start_rad(1) + (line1_end_rad(1) - line1_start_rad(1)) * t1;

    latIntersection_deg = rad2deg(latIntersection_rad);
    lonIntersection_deg = rad2deg(lonIntersection_rad);

    intersectionPoint = struct(&#39;latitude&#39;, latIntersection_deg, &#39;longitude&#39;, lonIntersection_deg);
end

huangapple
  • 本文由 发表于 2023年6月29日 23:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定