英文:
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
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) < 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
% 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('latitude', latIntersection_deg, 'longitude', lonIntersection_deg);
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论