# Normal distance from one curve to another at specific points

22 views (last 30 days)

Show older comments

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

Torsten
on 8 Jul 2022

### Answers (2)

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:

##### 4 Comments

Image Analyst
on 11 Jul 2022

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.

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

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!