MATLAB: Intersection of two Lines,which are passing through different slopes and two different points

geometry matlab

Hi,
I have two Lines.
Line1 has slope m1 and it passes through point (x1,y1).
Line2 has slope m2 and it passes through points (x2,y2).
I would like to find the intersection point of these two lines.
Thanks in advance.

Best Answer

m1 = rand ; %slope of line1
P1 = rand(2,1) ; % line1 passes through this point
%



m2 = rand ; % slope of line 2
P2 = rand(2,1) ; % line 2 passes through this point
% Get c
c1 = P1(2)-m1*P1(1) ;
c2 = P2(2)-m2*P2(1) ;
%
% MAke line coords
x = linspace(-10,10) ;
%
x1 = x ;
y1 = m1*x1+c1 ;
%
x2 = x ;
y2 = m2*x2+c2 ;
figure
hold on
plot(x1,y1) ;
plot(x2,y2) ;
%
P = InterX([x1;y1],[x2;y2]) ;
plot(P(1),P(2),'*r')