MATLAB: How to find the coordinate of the intersection multiple lines

intersectionlineMATLABtriangulation

Hi How to find multiple lines intersections. Example :
m=([0,1,2,3,5;9,10,12,12,10;5,0,6,4,1;5,7,4,9,1]);
x=([m(:,1),m(:,3)]); y=([m(:,2),m(:,4)]);
plot(x',y')

Best Answer

Interesting Friday afternoon problem!
The solution is reasonably straightforward, although not immediately obvious. The ‘Row’ designations correspond to the rows of ‘x’ and ‘y’, since that is the easiest way to refer to them. (The lines do not intersect in the original plot, so I assume the idea is to see where they would intersect if they were extended.)
First, estimate the parameters of each line:
for k = 1:size(x,1)
B(:,k) = [x(k,:)' ones(2,1)] \ y(k,:)'; % Parameters
end
Then determine the intersections:
for k1 = 1:size(B,2)
for k2 = 1:size(B,2)
XY(k1,k2,:) = [-B(1,[k1 k2])' [1;1]] \ B(2,[k1 k2])'; % Intersections
end
end
X = XY(:,:,1)
Y = XY(:,:,2)
Then tabulate the intersections:
Names = sprintfc('Row_%d',1:4);
XInt = table(X(:,1),X(:,2),X(:,3),X(:,4),'VariableNames',Names,'RowNames',Names)
YInt = table(Y(:,1),Y(:,2),Y(:,3),Y(:,4),'VariableNames',Names,'RowNames',Names)
XInt =
Row_1 Row_2 Row_3 Row_4
______ _____ ______ ______
Row_1 NaN 9 7 5.3333
Row_2 9 NaN 7.2 4.875
Row_3 7 7.2 NaN 6.1667
Row_4 5.3333 4.875 6.1667 NaN
YInt =
Row_1 Row_2 Row_3 Row_4
______ _____ ______ ______
Row_1 NaN 10 8 6.3333
Row_2 10 NaN 8.8 7.25
Row_3 8 8.8 NaN 4.6667
Row_4 6.3333 7.25 4.6667 NaN
And plot them:
xv = linspace(0,15);
Ym = B(1,:)'*xv + B(2,:)';
figure
plot(xv,Ym)
ylim([0 15])
grid
legend(Names, 'Location','SE')