Normal distance from one curve to another at specific points

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?

1 Comment

How are the two curves given ? In parametric form ? By a number of discrete points ? By a spline interpolation ?

Sign in to comment.

Answers (2)

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:

4 Comments

Confirmed, it works! 👍
t = linspace (pi/2, 3*pi/2, 3601);
x1 = 1.00*cos(t); % Curve 1
y1 = 1.00*sin(t);
x2 = 0.64*cos(t); % Curve 2
y2 = 0.64*sin(t);
plot(x1, y1, x2, y2)
xlim([-1 0]),
axis square
pbaspect([1 2 1])
index1 = 2401; % pick a point between 0 to 3601
distances = sqrt((x2 - x1(index1)).^2 + (y2 - y1(index1)).^2);
[minDistance, index2] = min(distances);
% Now you can fit a line through the two points.
xNormal = [x1(index1), x2(index2)];
yNormal = [y1(index1), y2(index2)];
hold on;
line(xNormal, yNormal, 'Color', [0.9290, 0.6940, 0.1250], 'LineWidth', 2);
hold off;
Thanks for the solution. I see that it draws a line between the closest point in the second curve and the indexed point in the first curve. This will cause problem if the closest point is not on the normal to the first curve since the distance is what you have considered here and not the normals.
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?
You can do it analytically instead of numerically like I did. That's a lot harder and I'm not going to write code for you to do that. You can run along the first curve, that you want the normals for, and look in a little window (say 11 elements) and fit some curve to it, like a quadratic. Then analytically compute the tangent line and the normal to the tangent. The tangent and normal equations will be lines. Now you have to extend that normal line and figure out where it goes between two points of your second line using interpolation since there is no second curve point exactly there. This would work for cases where the second curve points are widely spaced. My method works if the points are pretty close together, but it does draw a line to the two points exactly, not somewhere on a line segment between two points.
An alternative way is to just interpolate a bunch of points so that you have so many -- enough for the precision you desire. See attached demos. For example if your curve 2 has only a hundred points, use interp1 or spline to get a version of curve 2 with a million points. Then use my code. Hopefully that will be accurate enough for your needs.
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.

Sign in to comment.

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

Products

Release

R2020b

Asked:

on 8 Jul 2022

Edited:

on 11 Jul 2022

Community Treasure Hunt

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

Start Hunting!