# Intersection of Discretized Curves

33 views (last 30 days)

Show older comments

Prasanna Routray
on 21 Jun 2024

Edited: Star Strider
on 5 Oct 2024

Hi,

I have a scatter plot of two curves.

The curves intersect each other and ideally they should have one or more intersection points.

However, as I have discrete points and two curves do not intersect at any of those points, I can not get the point of intersection.

I'm attaching a figure for more clarity.

I believe I will have a set of points here instead a single point.

How do I get those set of points from the scatter plot data or the data points I have now?

Thanks..

##### 2 Comments

Tony
on 21 Jun 2024

### Accepted Answer

Star Strider
on 21 Jun 2024

It would help to have the data.

TThe approach to these problems is straightforward. In this instance, the independent variable values are different, so the second curve needs to to be interpoalted to the values of the first independent variable (or the reverse of this, depending on what you want to do). After that, it is relatively straightforward to interpolate to find the intersection. (If there are more than one intersection, it will first be necessary to find all the approximate indices of the intersections, and then loop to find the exact values of each intersection.)

Try this —

x1 = linspace(0, 0.06);

y1 = -x1.^2;

x2 = linspace(0, 0.058);

y2 = 1E-3 - x2.^2 * 1.5;

figure

plot(x1, y1, '.')

hold on

plot(x2, y2, '.')

hold off

y2i = interp1(x2, y2, x1, 'pchip', 'extrap'); % Values Of 'y2' Interpolated To 'x1'

intxx = interp1(y1-y2i, x1, 0) % Y-Coordinate Of Intersection

intxy = interp1(x1, y1, intxx) % X-Coordinate Of Intersection

figure

plot(x1, y1, '.-')

hold on

plot(x1, y2i, '.-')

plot(intxx, intxy, 'cs')

hold off

text(intxx, intxy, sprintf('$Intersection\\ at\\ (%.4f, %.4f) \\rightarrow$',intxx,intxy), 'Horiz','right', 'Interpreter','LaTeX')

This should work with your data, as written. I will of course help with any problems if necessary in adapting this to your data.

.

##### 6 Comments

Star Strider
on 5 Oct 2024

Edited: Star Strider
on 5 Oct 2024

It would be helpful to have a plot call or something similar, so I understand the result you want. (I added onee.)

Also, the code is incomplete. For example, the vectors ‘x1Coord’ and ‘y2Coord’ seem to be missing. Also, how would they be plotteed (since I assume they should be)?

Please provide the missing information, and explain what you want to do.

EDIT — (5 Oct 2024 at 16:16)

For two orthogonal lines, one with a zero slope and another with an infinite slope, the eassiest way would bee to use the ismember (or ismembertol) —

x1 = -100:1:100;

y1 = 5*ones(length(x1),1);

y2 = -100:1:100;

x2 = 5*ones(length(y2),1);

Lv = ismembertol(x1, y1, 1E-10);

Nv = find(Lv)

intxx = x1(Lv)

intxy = y1(Lv)

% y2i = interp1(x2', y2', x1', 'pchip', 'extrap'); % Values Of 'y2' Interpolated To 'x1'

% intxx = interp1(y1-y2i, x1,0) % Y-Coordinate Of Intersection

% intxy = interp1(x1, y1, intxx)

figure

plot(x1, y1)

hold on

plot (x2, y2)

plot(intxx, intxy, 'cs', 'MarkerSize',10)

hold off

grid

The typical intersection approaches, such as in my original answer, will not work in this instance. They will only work with finite slopes.

.

### More Answers (1)

Bruno Luong
on 5 Oct 2024

Edited: Bruno Luong
on 5 Oct 2024

Use this file exchange https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections

This code is robust, it doesn't break for repeating x or y coordinates.

The curves are assumed to me line segments connectings data points. Intersection coordinates of two line segments that meet are returned, and all of them.

x1 = -100:1:100;

y1 = 5*ones(size(x1));

y2 = -100:1:100;

x2 = 5*ones(size(y2));

X = InterX([x1; y1],[x2; y2]);

plot(x1,y1,'-',x2,y2,'-.',X(1,:),X(2,:),'or')

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!