# How to find the point of intersection of a line and a curve?

14 views (last 30 days)
Venkata Bhargav Tanguturi on 22 Apr 2018
Answered: John BG on 22 Apr 2018
Hi, I wrote a code to plot few straight lines and a and a curve made of scattered points. The data set and the code is as follows.
for n=1:1:33
xn=[Transects(n,1) Transects(n,3)];
yn=[Transects(n,2) Transects(n,4)];
plot (xn,yn,'*')
hold on
line (xn,yn)
hold on
end
k=1;
for i=1:2:2146
xi=Shorelines(i,1);
yi=Shorelines(i,2);
xj=Shorelines(i+1,1);
yj=Shorelines(i+1,2);
a1=[xi,xj];
b1=[yi,yj];
Interm(k,1)= xi;
Interm(k,2)= yi;
Interm(k,3)= xj;
Interm(k,4)= yj;
k=k+1;
end
for r=1:1:1073
xn1=[Interm(r,1) Interm(r,3)];
yn1=[Interm(r,2) Interm(r,4)];
plot(Interm(r,1),Interm(r,2),'.');
plot(Interm(r,3),Interm(r,4),'.');
hold on
if r<1073
xn2=[Interm(r,3) Interm(r+1,1)];
yn2=[Interm(r,4) Interm(r+1,2)];
line (xn2,yn2);
end
line (xn1,yn1)
hold on
end
grid on
title('Finding Intersections of Functions')
xlabel('Easting')
ylabel('Northing')
Now I would like to find the point of intersection of the two lines. Any help would be appreciated. Thanks

John BG on 22 Apr 2018

Hi Venkata

this is John BG jgb2012@sky.com

In order to simplify the acquisition of data from the .csv files you have supplied I've had to modify Shoreline.csv removing all non-figures cells.

Please find attached copy of this script along with the .csv files I used, and an early version of Mr Douglas Schwarz function intersections.m needed again to readily get the intersecting points of the doubled seashore with each segment.

https://uk.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections?s_tid=srchtitle

So,

the start point is .

1.-

Acquiring data

```clear all;close all;clc
```
```Transects=csvread('Transects.csv')
dc=3                                                   % amount decimals precision
```

.

2.-

Getting the points of each Transects segment

```L={}
for n=1:1:33
xn=[Transects(n,1) Transects(n,3)];
yn=[Transects(n,2) Transects(n,4)];
plot(xn,yn,'*')
hold on
hl=plot(xn,yn,'b-')
```

3.-

Capturing segment points needed to get to intersections

```kx=10^-dc*floor(linspace(10^dc*xn(1,1),...
10^dc*xn(1,2),...
max(abs(10^dc*xn(1,1)-10^dc*xn(1,2)),abs(10^dc*yn(1,1)-10^dc*yn(1,2)))));
```
```ky=10^-dc*floor(linspace(10^dc*yn(1,1),...
10^dc*yn(1,2),...
max(abs(10^dc*xn(1,1)-10^dc*xn(1,2)),abs(10^dc*yn(1,1)-10^dc*yn(1,2)))));
pkxy=[kx' ky'];
L=[L pkxy];
```
```plot(kx,ky,'r*');   % just checking
```
```% hl=line(xn,yn)   % I like plot
% hl.XData           % neither handles to plot or line returns contain more than the tip points
% hl.YData
```
```end
``` .

4.-

Acquiring the cutting line, the Seashore-like line

```k=1;
```
```for i=1:2:2146
xi=Shorelines(i,1);
yi=Shorelines(i,2);
xj=Shorelines(i+1,1);
yj=Shorelines(i+1,2);
a1=[xi,xj];
b1=[yi,yj];
Interm(k,1)= xi;
Interm(k,2)= yi;
Interm(k,3)= xj;
Interm(k,4)= yj;
k=k+1;
end
```
```L2=[0 0];
for r=1:1:1073                                                      % plotting Shoreline along with a segment to disregard.
xn1=[Interm(r,1) Interm(r,3)];
yn1=[Interm(r,2) Interm(r,4)];
plot(Interm(r,1),Interm(r,2),'.');
plot(Interm(r,3),Interm(r,4),'.');
hold on
if r<1073
xn2=[Interm(r,3) Interm(r+1,1)];
yn2=[Interm(r,4) Interm(r+1,2)];
line(xn2,yn2);
end
%  line(xn1,yn1)
plot(xn1(1),yn1(1),'g*')  %  just checking
L2=[L2;xn1(1) yn1(1)];
hold on
end
L2(1,:)=[];
``` .

Now L2 contains all points of the sea shore line.

5.-

There are 33 Transect segments, let's find the intersecting points of each Transect segment with the sea shore

```L2nx=L2(:,1);
L2ny=L2(:,2);
Xp1=[0 0];
Xp2=[0 0];
for k=1:1:size(L,2)
L1=L{k};
L1nx=L1(:,1);
L1ny=L1(:,2);
[x0n,y0n]=intersections(L2nx,L2ny,[L1nx(1) L1nx(end)],[L1ny(1) L1ny(end)]);
x0n=x0n';y0n=y0n';
% expecting 1 and only 1 intersection point from each segment on seashore line, otherwise cell required.
plot(x0n(1),y0n(1),'bd')
plot(x0n(2),y0n(2),'yd')
Xp1=[Xp;x0n(1) y0n(1)];   % yellow intersections curved sea shore
Xp2=[Xp;x0n(2) y0n(2)];   % blue intersections, approximation of sea shore with additional straight segment.
end
Xp(1,:)=[];Xp2(1,:)=[];       % some habits die hard
``` .

The variables

Xp1 Xp2

.

Contain the sought intersections.

Comment: there's a short lines crossing, points Xp1 Xp2, on the left hand side of the sea shore.

Since you define the seashore line with both a detailed contour and segments approximation, there's need to know if you want to discard one of them, or it's ok as it is now.

Venkata

thanks in advance for time and attention

John BG

jgb2012@sky.com