How do I find the intersection point of two lines in MATLAB 6.5 (R13)?
3 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2009-6-27
编辑: MathWorks Support Team
2020-4-30
How do I find the intersection point of two lines in MATLAB 6.5 (R13)?
采纳的回答
MathWorks Support Team
2020-4-30
编辑:MathWorks Support Team
2020-4-30
Currently, there is no function in MATLAB that allows you to find intersection of any two lines or line segments. If you know that two lines in 2D intersect (are not skew) and you know two points on each of those lines, you can find the intersection using the following formula:
% Sample data
L1_x1 = 1;
L1_y1 = 2;
L1_x2 = 3;
L1_y2 = 4;
L2_x1 = 1;
L2_y1 = 4;
L2_x2 = 3;
L2_y2 = 3;
% Plot the lines
plot([L1_x1 L1_x2], [L1_y1 L1_y2])
hold on
plot([L2_x1 L2_x2], [L2_y1 L2_y2])
% Compute several intermediate quantities
Dx12 = L1_x1-L1_x2;
Dx34 = L2_x1-L2_x2;
Dy12 = L1_y1-L1_y2;
Dy34 = L2_y1-L2_y2;
Dx24 = L1_x2-L2_x2;
Dy24 = L1_y2-L2_y2;
% Solve for t and s parameters
ts = [Dx12 -Dx34; Dy12 -Dy34] \ [-Dx24; -Dy24];
% Take weighted combinations of points on the line
P = ts(1)*[L1_x1; L1_y1] + (1-ts(1))*[L1_x2; L1_y2];
Q = ts(2)*[L2_x1; L2_y1] + (1-ts(2))*[L2_x2; L2_y2];
% Plot intersection points
plot(P(1), P(2), 'ro')
plot(Q(1), Q(2), 'bo')
hold off
P and Q both contain the values of the common intersection point.
Alternately, if you have discrete data and know that some point is in data sets for both lines, you can use the "intersect" function.
Finally, if you own the "Mapping Toolbox" for MATLAB, you can use the "polyxpoly" function to calculate the intersection point.
For more information on these or any other command, type the following at the MATLAB command prompt:
help function_name
% or
doc function_name
%where function_name is the name of the function of interest
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!