How do I find the intersection point of two lines in MATLAB 6.5 (R13)?

70 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Apr 2020
Edited: MathWorks Support Team on 30 Apr 2020
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!