Intersection of Discretized Curves

31 visualizaciones (últimos 30 días)
Prasanna Routray
Prasanna Routray el 21 de Jun. de 2024
Editada: Star Strider el 5 de Oct. de 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 comentarios
Tony
Tony el 21 de Jun. de 2024
The pair of points on the two curves with the smallest Euclidean distance between them will be the intersection point. If you are looking for multiple, then you can put a threshold; e.g. find the pairs of points whose distance is less than 1e-6
Prasanna Routray
Prasanna Routray el 21 de Jun. de 2024
Hi.. thanks for the reply. How do I do that?

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 21 de Jun. de 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
intxx = 0.0447
intxy = interp1(x1, y1, intxx) % X-Coordinate Of Intersection
intxy = -0.0020
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 comentarios
Star Strider
Star Strider el 5 de Oct. de 2024
Editada: Star Strider el 5 de Oct. de 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)
Nv = 106
intxx = x1(Lv)
intxx = 5
intxy = y1(Lv)
intxy = 5
% 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.
.
Prasanna Routray
Prasanna Routray el 5 de Oct. de 2024
Hi...
The idea was to plot the two lines, get the point of intersection and plot the intersection point.
yes, the code had some typo. I edited the above one and posted the new code below.
x1 = -100:1:100;
y1 = 5*ones(length(x1),1);
y2 = -100:1:100;
x2 = 5*ones(length(y2),1);
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)
% the following should plot everything if all goes well
figure
plot(x1, y1)
hold on
plot (x2, y2)
plot(intxx, intxy,'or')
hold off
grid

Iniciar sesión para comentar.

Más respuestas (1)

Bruno Luong
Bruno Luong el 5 de Oct. de 2024
Editada: Bruno Luong el 5 de Oct. de 2024
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')

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by