Calculating distance between 2 curves

9 views (last 30 days)
Rens Bos
Rens Bos on 30 Jun 2023
Commented: Image Analyst on 1 Jul 2023
Hi,
For a project, I need to determine the distance between two curves. The magenta colored linesegments should be perpendicular to the 'approximation' (green) curve. What I have right now is that the line segments are indeed perpendicular, however the distance is calculated between the (green) 'approximation' curve and the 'greentyre'(blue), while I want the distance between 'Carcass with belts'(red) and 'greentyre'(blue) curve.
Hopefully you understand my question. Could someone help me with my code?
Thanks in advance!
Beneath is my code:
clc, clear, clf
% Load data from Excel
data1 = xlsread('Data_tread_material_scan.xlsx');
data2 = xlsread('Thickness_caliper.xlsx');
x = data1(:, 1); % x values of the measurements
carcass = data1(:, 2); % y values of carcass
greentyre = data1(:, 3); % y values of greentyre
thickness_caliper = data2(:,1);
degree = 10;
c_carcass = polyfit(x, carcass, degree); % Approximate a polynomial of degree #degree from the y-carcass datapoints
c_greentyre = polyfit(x, greentyre, degree); % Approximate a polynomial of degree #degree from the y-greentyre datapoints
y_carcass = polyval(c_carcass, x); % Make a polynomial of degree #degree from the y-carcass datapoints
y_greentyre = polyval(c_greentyre, x); % Make a polynomial of degree #degree from the y-greentyre datapoints
% index of point
ind = linspace(1,length(x)-1,length(x)-1)'; % 10 random points
XC = ind * nan;
YC = ind * nan;
% Create an empty vector to store the lengths
lengths = [];
for i = 1:length(ind)
ix = ind(i);
x0 = x(ix);
y0 = y_carcass(ix);
% tangent vector
dy = diff(y_carcass(ix:ix+1));
dx = diff(x(ix:ix+1));
% normal vector
xv = [0 -dy] * 50 + x0;
yv = [0 dx] * 50 + y0;
% intersection point
[xc, yc] = polyxpoly(xv, yv, x, greentyre);
if ~isempty(xc)
XC(i) = xc;
YC(i) = yc;
% Calculate the length of the red line segment
length_i = sqrt((x(ind(i)) - xc)^2 + (y_carcass(ind(i)) - yc)^2);
% Store the length in the vector
lengths = [lengths, length_i];
end
end
figure(1) % defining figure 1
hold on
title('Distance')
xlabel('Width [mm]')
ylabel('Thickness [mm]')
plot(x, y_carcass, 'color', 'green', 'linewidth', 2)
plot(x, carcass, 'color', 'red', 'linewidth', 2)
plot(x, greentyre, 'color', 'blue', 'linewidth', 2)
plot([XC x(ind)]', [YC y_carcass(ind)]', 'color','magenta')
legend('Approximation','Carcass with belts','Greentyre')
axis([200 950 650 800])
hold off
axis equal
x2 = linspace(x(1),x(end),71)
figure(2) % defining figure 2
hold on
plot(x',[0,lengths,0]) % plot of the Matlab calculations
plot(x2, thickness_caliper) % plot of the caliper measurement
title('Thickness tread material')
xlabel('Width [mm]')
ylabel('Thickness [mm]')
legend('Matlab approximation','Caliper')
hold off
axis equal
  2 Comments
Matt J
Matt J on 30 Jun 2023
Edited: Matt J on 30 Jun 2023
It's not clear to me how you are defining the red-to-blue distance. You say the magenta lines correctly connect the green curve to the blue. But what relevance are the magenta lines supposed to have to the red curve and to the red-blue distance calculation?
Image Analyst
Image Analyst on 1 Jul 2023
Unfortunately you forgot to read the posting guidelines and thus forgot to attach your workbooks, thus delaying an answer. We'll check back later for it. Not sure what you mean by distance? Just one overall average distance? Or a distance for each point on the upper and lower curves to the closest point on the other curve. your distance lines are not perpendicular, but that's OK - I don't think they should be anyway.

Sign in to comment.

Answers (1)

Matt J
Matt J on 30 Jun 2023
If you are trying to find the intersections of the magenta lines (extended to infinity) with the red curve, then this might help:

Community Treasure Hunt

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

Start Hunting!