# Normal distance from one curve to another at specific points

16 views (last 30 days)
Biraj Khanal on 8 Jul 2022
Edited: Torsten on 11 Jul 2022
If I have two curves with datapoints, how can I get the distance from any point in one curve to the another one? I am trying to do it with the following steps:
Find normals to the reference curve (red one) at each point and then find the points where the normals intersect the target curve (blue here). But I cannot put it in code. Also, I want to use the normals for the next step. So, is there a way to get equations of the normals at each points?
Torsten on 8 Jul 2022
How are the two curves given ? In parametric form ? By a number of discrete points ? By a spline interpolation ?

Image Analyst on 8 Jul 2022
If you have 4 vectors - curve1x, curve1y, curve2x, curve2y - and a point at some point, index1, in curve1, then you can find the closest point in curve 2 to that point.
distances = sqrt((curve2x - curve1x(index1)) .^ 2 + (curve2y - curve1y(index1)) .^ 2);
[minDistance, index2] = min(distances);
% Now you can fit a line through the two points.
xNormal = [curve1x(index1), curve2x(index2)];
yNormal = [curve1y(index1), curve2y(index2)];
hold on;
line(xNormal, yNormal, 'Color', 'r', 'LineWidth', 2);
hold off;
coefficients = polyfit(xNormal, yNormal, 1); % Get equation of a line
So does that do what you want?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Torsten on 11 Jul 2022
Edited: Torsten on 11 Jul 2022
What I am trying to do is a little different. I am trying to find the normal lines on each point of the reference curve1x, curve1y. Then, I am trying to find where the normal intersects the second curve curve2x,curve2y. So, is there a way to go about this with the normals to start with?
Why do you start with the normal for the reference curve and not with the normal for the second curve ? You method does not necessarily yield the minimum distance.
Use "cscvn" to construct a spline through the points of both curves. Let's call them sp1(t) and sp2(t). Then try to minimize
d(t) = (sp1x(t0) - sp2x(t))^2 + (sp1y(t0) - sp2y(t))^2
for t0 indicating a fixed point P0 on sp1.
Use e.g. fminsearch to solve this optimization problem.

Sam Chak on 8 Jul 2022
If the equations of the curves are unknown, then I guess you can find the Tangent lines to the curves using the gradient() function. From the Tangent lines, you can find the Normal lines. This is what I remember from the Calculus Textbook by Ron Larson & Bruce Edwards. ### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!