# Multiple curves and line intersection.

332 Views

Matlab is the best computation and quantitative solving software to analyzing and use many impossible things to possible. Line intersection used for many geometric problems.

like 1. find the difference between two points.

2.ploting function to plot the image graph and then object measurement using distance functions

### How to intersect and count intersection line points

``````%% http://www.yawot.com/

%Author-Rahul
%date=5/apr/2019
%this file contain matlab file exchange code to make easy understanding how
%to multiple line intersecting using excell data sheet and line draw
%intersection.
%%filename = 'gg.csv';
%examplen editable any time
%x1 = num(:,1) ; y1 =  num(:,2) ;
%x2=[0 100 150 200 400 700];
%y2=[56 68 70 70 68 32];
%x3=[0 20 150 200 300];
%y3=[70 77  77 78 79 ];
%%
function P1= InterX1(L1,varargin)
filename = 'gg.csv';
x1 = num(:,1) ; y1 =  num(:,2) ;
x2=[0 100 150 200 400 700];
y2=[56 68 70 70 68 32];
x3=[0 20 150 200 300];
y3=[70 77  77 78 79 ];
L1 = [x1 y1]' ;
L2 = [x2 ; y2] ;
L3 = [x3 ; y3] ;
plot(x1,y1);
plot(x3,y3);
plot(x2,y2);
P1 =InterX(L1,L2) ;
P2 =InterX(L1,L3) ;
plot(x1,y1,'r')
hold on
plot(x3,y3,'r')
bc=plot(x2,y2,'b');
ac=plot(P1(1,:),P1(2,:),'*r');
Ab=plot(P2(1,:),P2(2,:),'*b');
function P = InterX(L1,varargin)

error(nargchk(1,2,nargin));
if nargin == 1,
L2 = L1;    hF = @lt;   %...Avoid the inclusion of common points
else
L2 = varargin{1}; hF = @le;
end

%...Preliminary stuff
x1  = L1(1,:)';  x2 = L2(1,:);
y1  = L1(2,:)';  y2 = L2(2,:);
dx1 = diff(x1); dy1 = diff(y1);
dx2 = diff(x2); dy2 = diff(y2);

%...Determine 'signed distances'
S1 = dx1.*y1(1:end-1) - dy1.*x1(1:end-1);
S2 = dx2.*y2(1:end-1) - dy2.*x2(1:end-1);

C1 = feval(hF,D(bsxfun(@times,dx1,y2)-bsxfun(@times,dy1,x2),S1),0);
C2 = feval(hF,D((bsxfun(@times,y1,dx2)-bsxfun(@times,x1,dy2))',S2'),0)';
%...Obtain the segments where an intersection is expected
[i,j] = find(C1 & C2);
if isempty(i),P = zeros(2,0);return; end;

%...Transpose and prepare for output
i=i'; dx2=dx2'; dy2=dy2'; S2 = S2';
L = dy2(j).*dx1(i) - dy1(i).*dx2(j);
i = i(L~=0); j=j(L~=0); L=L(L~=0);  %...Avoid divisions by 0

%...Solve system of eqs to get the common points
P = unique([dx2(j).*S1(i) - dx1(i).*S2(j), ...
dy2(j).*S1(i) - dy1(i).*S2(j)]./[L L],'rows')';

function u = D(x,y)
u = bsxfun(@minus,x(:,1:end-1),y).*bsxfun(@minus,x(:,2:end),y);
end
end
end`````` 