# The lowest distance of two points on two curves

1 view (last 30 days)
Dominik Cech on 12 Jan 2022
Edited: John D'Errico on 12 Jan 2022
Hello,
I have two curves. Each is made of 100 points with X, Y and Z coordination. How do I find for each point on one curve point on the second one which has smallest distance betwen them? Someone told I have to brute force it but I realy dont know how to do it.
Torsten on 12 Jan 2022
Take the first point on curve 1, calculate the distance to each of the 100 points on the other curve and take the minimum of these distances.
Proceed with the second point on curve 1.

John D'Errico on 12 Jan 2022
Edited: John D'Errico on 12 Jan 2022
PDIST, from the stats toolbox can compute the set of all interpoint distances between two sets of data. Then you could just take the smallest distance for each point on curve 1.
You can also use my IPDM, as found on the file exchange. But by far the best is to use KNNSEARCH. For example...
t = linspace(0,1)';
XYZ1 = [sin(t),cos(t),sin(2*t)];
XYZ2 = [t,t.^2,t.^3];
plot3(XYZ1(:,1),XYZ1(:,2),XYZ1(:,3),'r-')
hold on
plot3(XYZ2(:,1),XYZ2(:,2),XYZ2(:,3),'g-')
grid on
box on
IDX = knnsearch(XYZ1,XYZ2);
plot3([XYZ1(:,1)';XYZ2(IDX,1)'],[XYZ1(:,2)';XYZ2(IDX,2)'],[XYZ1(:,3)';XYZ2(IDX,3)'],'b-')
So every blue line connected a point on the red curve to its closest cousin on the green curve. (Note that the closest point on the red curve, for any point on the green curve is a not a symmetric thing.)
Do any of these solutions find the closest point on the CURVE? NO!!!!!!! All you have are isolated points along a curve, not the curve itself. I do have a tool on the file exchange that does solve that problem, by finding the closest point on a spline interpolant of your curve. That is the distance2curve function. knnsearch should be adequate though.

R2020b

### Community Treasure Hunt

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

Start Hunting!